1

I have a dataframe in Python of dimensions 21392x1972. What I would like to accomplish is to convert the data frame into a list of lists, such that the first column of my data frame is the first list within the long list, second column of data frame the second list with the one long list and so on.

I tried to use tolist() to convert the data frame to a list of lists. What is happening now is that each row of my data frame is becoming one list within the long list. But, what I would like to accomplish is that each column of data frame should become one list within the long list. I am new to using Pandas and Python, so any help in this regard is highly appreciated. Cheers!

import pandas as pd
mydataset = pd.read_csv('final_merged_data.csv')
mydataset_seq = mydataset.values.tolist() 
1
  • transpose your dataframe and export it afterwards? but why would you leave pandas for normal list ops? Commented Jan 17, 2019 at 21:41

2 Answers 2

3

Loop through all columns in your dataframe, index that column, and convert it to a list:

lst = [df[i].tolist() for i in df.columns]

Example:

df = pd.DataFrame({'a' : [1, 2, 3, 4],
'b' : [5, 6, 7, 8]})

print(df)
print('list', [df[i].tolist() for i in df.columns])

Output:

   a  b
0  1  5
1  2  6
2  3  7
3  4  8
'list' [[1, 2, 3, 4], [5, 6, 7, 8]]
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you so much. Can you also kindly tell how I can retain my column names in dataframe while converting it into lists? Right now using tolist() removes the column names as per my code. Actually all my columns in dataframes have names and I would like to retain the column names in the list of lists.
I'm so sorry, I legitimately didn't see this comment. Do you still need this?
it would be wonderful if you could help with this please. Basically, I want to know if there is a way to retain column names in the list of lists?
You really just have to get each column name from df.columns: [[i] + df[i].tolist() for i in df.columns])
Thanks. Does this mean I am converting df to a list and assigning it to the columns of df? What is df.columns: doing here, can you please clarify. Sorry to ask, but I just wanted to know why assignment operator = is not used here.
|
2

Just transpose what you have

mydataset_seq = mydataset.T.values.tolist() 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.