矢量我有使用熊猫一个数据帧:如何提取GROUPBY大熊猫蟒蛇
one two three
1 2 1
4 1 1
2 2 1
3 1 2
20 2 2
现在,我将通过分组“三”提取的向量。 基本上,我应该基于分组“三”从“两”列得到向量:
groupby('three')
a=[2,1,2]
b=[1,2]
非常感谢
矢量我有使用熊猫一个数据帧:如何提取GROUPBY大熊猫蟒蛇
one two three
1 2 1
4 1 1
2 2 1
3 1 2
20 2 2
现在,我将通过分组“三”提取的向量。 基本上,我应该基于分组“三”从“两”列得到向量:
groupby('three')
a=[2,1,2]
b=[1,2]
非常感谢
您可以使用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]]
完美的,伟大的。但是,如何使用结果,它是一个DataFrame?基本上,我如何管理结果载体? – user7311536
这意味着什么?输出为['Series'](http://pandas.pydata.org/pandas-docs/stable/dsintro.html#series) – jezrael
非常感谢。我把输出放在DataFrame的列内,现在我要用plot.box来绘制我们已经获得的数组,你能帮我吗?获得一个阴谋作为一个盒子剧情序列 – user7311536
重复数据删除:https://stackoverflow.com/questions/22219004/grouping-rows-in-list-in-pandas-groupby – EdChum