python DataFrame groupby用法

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_group是DataFrameGroupBy的方法,不是DataFrame的方法

 

你可能感兴趣的:(python DataFrame groupby用法)