I have a pandas DataFrame with multiple columns.
2u 2s 4r 4n 4m 7h 7v 0 1 1 0 0 0 1 0 1 0 1 0 0 1 1 0 0 1 0 1 0 1 0 0 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 0 0 1
What I want to do is to convert this pandas.DataFrame
into a list like following
X = [ [0, 0, 1, 1, 1, 0], [1, 1, 0, 0, 0, 1], [1, 0, 0, 0, 1, 1], [0, 1, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0], [0, 0, 1, 1, 1, 0], [1, 1, 0, 0, 0, 1] ]
2u 2s 4r 4n 4m 7h 7v are column headings. It will change in different situations, so don't bother about it.
values. tolist() you can convert pandas DataFrame Column to List. df['Courses'] returns the DataFrame column as a Series and then use values. tolist() to convert the column values to list.
Index column can be converted to list, by calling pandas. DataFrame. index which returns the index column as an array and then calling index_column. tolist() which converts index_column into a list.
Dataframe() function to declare the DataFrame from the dictionary and then use the tolist() function to convert the Dataframe to a list containing all the rows of column 'emp_name'. Once you will print 'd' then the output will display in the form of a list.
To access the names of a Pandas dataframe, we can the method columns(). For example, if our dataframe is called df we just type print(df. columns) to get all the columns of the Pandas dataframe. After this, we can work with the columns to access certain columns, rename a column, and so on.
It looks like a transposed matrix:
df.values.T.tolist()
[list(l) for l in zip(*df.values)]
[[0, 0, 1, 1, 1, 0], [1, 1, 0, 0, 0, 1], [1, 0, 0, 0, 1, 1], [0, 1, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0], [0, 0, 1, 1, 1, 0], [1, 1, 0, 0, 0, 1]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With