如何从python中的groupby pandas中提取一个向量

时间:2017-04-04 14:41:42

标签: python pandas

我有一个使用pandas的DataFrame:

one    two  three

 1      2    1 
 4      1    1
 2      2    1
 3      1    2
 20     2    2

现在,我将通过分组“三个”来提取a向量。 基本上,我应该从“&#;基于分组的列"三":

groupby('three')
a=[2,1,2]
b=[1,2]

非常感谢

1 个答案:

答案 0 :(得分:5)

您可以使用groupby

s = df.groupby('three')['two'].apply(list)
print (s)
three
1    [2, 1, 2]
2       [1, 2]
Name: two, dtype: object

a = s.loc[1]
b = s.loc[2]
print (a)
[2, 1, 2]

print (b)
[1, 2]

如果需要嵌套列表:

L = df.groupby('three')['two'].apply(list).tolist()
print (L)
[[2, 1, 2], [1, 2]]

另一种可能的解决方案:

L = [list(x) for i, x in df.groupby('three')['two']]
print (L)
[[2, 1, 2], [1, 2]]

L = [x.tolist() for i, x in tuple(df.groupby('three')['two'])]
print (L)
[[2, 1, 2], [1, 2]]