python DataFrame groupbyの使い方

954 ワード

df = pd.DataFrame({'key1':list('aabba'), 'key2': ['one','two','one','two','one'], 'data1': ['1','3','5','7','9'], 'data2': ['2','4','6','8','10']}) 
print df
out:
  data1 data2 key1 key2
0     1     2    a  one
1     3     4    a  two
2     5     6    b  one
3     7     8    b  two
4     9    10    a  one
grouped = df.groupby('key1')
print grouped
out:

grouped_1 = grouped.get_group('a').reset_index()#get_group group,reset_index    index
print grouped_1
   index data1 data2 key1 key2
0      0     1     2    a  one
1      1     3     4    a  two
2      4     9    10    a  one

get_グループはDataFrameGroup Byの方法であり、DataFrameの方法ではありません