2017-04-04 8 views
4

でGROUPBYパンダからベクトルを抽出する方法:今私はパンダを使用してDATAFRAME持っているのpython

one two three 

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

を、私は「3」をグループ化することによってベクトルを抽出します。 基本的に、私は「3」をグループ化に基づいて「2」列からベクトルを取得する必要があります。

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

おかげでたくさん

+0

デュープ:https://stackoverflow.com/questions/22219004/grouping-rows-in-list-in-pandas-groupby – EdChum

答えて

4

をあなたが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]] 

もう1つの解決策:

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]] 
+0

パーフェクト、素晴らしいです。しかし、どのように私は結果を使用することができます、それはDataFrameですか?基本的に、結果のベクトルをどのように管理できますか? – user7311536

+1

管理の意味は?出力は['Series'](http://pandas.pydata.org/pandas-docs/stable/dsintro.html#series) – jezrael

+0

ありがとうございます。私はDataFrameの列内に出力を入れましたが、私が得た配列のplot.boxでプロットするようになりました。どうすれば助けてくれますか?ボックスプロットのシーケンスとしてプロットを取得 – user7311536

関連する問題