Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert pandas DataFrame into list of lists [duplicate]

I have a pandas data frame like this:

admit   gpa  gre  rank    0  3.61  380     3   1  3.67  660     3   1  3.19  640     4   0  2.93  520     4 

Now I want to get a list of rows in pandas like:

[[0,3.61,380,3], [1,3.67,660,3], [1,3.19,640,4], [0,2.93,520,4]]    

How can I do it?

like image 785
user2806761 Avatar asked Oct 25 '13 08:10

user2806761


2 Answers

There is a built in method which would be the fastest method also, calling tolist on the .values np array:

df.values.tolist()  [[0.0, 3.61, 380.0, 3.0],  [1.0, 3.67, 660.0, 3.0],  [1.0, 3.19, 640.0, 4.0],  [0.0, 2.93, 520.0, 4.0]] 
like image 108
EdChum Avatar answered Sep 30 '22 17:09

EdChum


you can do it like this:

map(list, df.values) 
like image 28
Roman Pekar Avatar answered Sep 30 '22 17:09

Roman Pekar