简体   繁体   English

如何在Pandas中使用group-by?

[英]How to use group-by in Pandas?

My data-frame look like this(two columns col1,col2) 我的数据框看起来像这样(两列col1,col2)

1 100
2 150
3 170
1 200

I want to do group-by col1, 我想分组col1,

pd.DataFrame(combined.groupby('col1').aggregate(np.mean)['col2'])

This is returning a data-frame with only one key col2, I, actually, want the output to be like this( dataframe with two columns) 这是返回一个只有一个键col2的数据帧,实际上,我希望输出像这样(带有两列的数据帧)

col1,mean(col2), 

could somebody point out what do I have to achive this? 有人可以指出我有什么要做到这一点?

You can use groupby with aggregating mean and reset_index : 您可以将groupby与聚合meanreset_index

print df.groupby('col1')['col2'].mean().reset_index()
   col1  col2
0     1   150
1     2   150
2     3   170

Solution with groupby with paameter as_index=False as mentioned John Galt : 使用groupby和paameter的解决方案as_index=FalseJohn Galt

print df.groupby('col1', as_index=False)['col2'].mean()
   col1  col2
0     1   150
1     2   150
2     3   170

Solution with aggregate : aggregate解决方案:

print df.groupby('col1', as_index=False).aggregate({'col2':'mean'})
   col1  col2
0     1   150
1     2   150
2     3   170

Aggregation in docs 文档中的聚合

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM