I have a pandas data frame like this:
我有一个像这样的pandas数据框:
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:
现在我想得到一个pandas中的行列表,如:
[[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? please help me. Thanks a lot.
我该怎么做?请帮帮我。非常感谢。
3 个解决方案
#1
60
You can achieve this using iterrows
:
您可以使用iterrows实现此目的:
temp=[]
for row in df.iterrows():
index, data = row
temp.append(data.tolist())
Alternatively you can also use apply
:
或者您也可以使用申请:
df.apply(lambda x: x.tolist(), axis=1)
Update
更新
After looking at this again there is a built in method which would be the fastest method also, calling tolist
on the .values
np array:
再看一遍后,有一个内置的方法也是最快的方法,在.values np数组上调用tolist:
In [62]:
df.values.tolist()
Out[62]:
[[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]]
#2
21
you can do it like this:
你可以这样做:
map(list, df.values)
#3
5
You can use the built in as_matrix
method on the dataframe (reference: 1) :
您可以在数据帧上使用内置的as_matrix方法(参考:1):
In [8]:
df.as_matrix()
Out[8]:
array([[ 0.9, 7. , 5.2, ..., 13.3, 13.5, 8.9],
[ 0.9, 7. , 5.2, ..., 13.3, 13.5, 8.9],
[ 0.8, 6.1, 5.4, ..., 15.9, 14.4, 8.6],
...,
[ 0.2, 1.3, 2.3, ..., 16.1, 16.1, 10.8],
[ 0.2, 1.3, 2.4, ..., 16.5, 15.9, 11.4],
[ 0.2, 1.3, 2.4, ..., 16.5, 15.9, 11.4]])
#1
60
You can achieve this using iterrows
:
您可以使用iterrows实现此目的:
temp=[]
for row in df.iterrows():
index, data = row
temp.append(data.tolist())
Alternatively you can also use apply
:
或者您也可以使用申请:
df.apply(lambda x: x.tolist(), axis=1)
Update
更新
After looking at this again there is a built in method which would be the fastest method also, calling tolist
on the .values
np array:
再看一遍后,有一个内置的方法也是最快的方法,在.values np数组上调用tolist:
In [62]:
df.values.tolist()
Out[62]:
[[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]]
#2
21
you can do it like this:
你可以这样做:
map(list, df.values)
#3
5
You can use the built in as_matrix
method on the dataframe (reference: 1) :
您可以在数据帧上使用内置的as_matrix方法(参考:1):
In [8]:
df.as_matrix()
Out[8]:
array([[ 0.9, 7. , 5.2, ..., 13.3, 13.5, 8.9],
[ 0.9, 7. , 5.2, ..., 13.3, 13.5, 8.9],
[ 0.8, 6.1, 5.4, ..., 15.9, 14.4, 8.6],
...,
[ 0.2, 1.3, 2.3, ..., 16.1, 16.1, 10.8],
[ 0.2, 1.3, 2.4, ..., 16.5, 15.9, 11.4],
[ 0.2, 1.3, 2.4, ..., 16.5, 15.9, 11.4]])