Pandas group by 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20851225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:29:48  来源:igfitidea点击:

Pandas group by will not work

pythonpandas

提问by Tampa

what am i missing here? I am trying to do a group by.

我在这里错过了什么?我正在尝试做一个小组。

asp = np.array(np.array([0,0,1]))
asq = np.array(np.array([10,10,20]))
columns=['asp']
df = pd.DataFrame(asp, index=None, columns=columns)
df['asq'] = asq
print df
df.groupby(by=['asp']).sum()
print df
   asp  asq
0    0   10
1    0   10
2    1   20
   asp  asq
0    0   10
1    0   10
2    1   20

results should be:

结果应该是:

    asp  asq
0    0   20
1    1   20

回答by DSM

df.groupbydoesn't change df; it returns a new object. In this case you perform an aggregation operation, so you get a new DataFrame. You have to give a name to the result if you want to use it later:

df.groupby不会改变df;它返回一个新对象。在这种情况下,您执行聚合操作,因此您将获得一个新的DataFrame. 如果您想稍后使用它,则必须为结果命名:

>>> df_summed = df.groupby('asp').sum()
>>> df_summed
     asq
asp     
0     20
1     20

[2 rows x 1 columns]