pandas groupby存储在一个新的数据框中 [英] Pandas groupby stored in a new dataframe

查看:65
本文介绍了 pandas groupby存储在一个新的数据框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

import pandas as pd
df1 = pd.DataFrame({'Counterparty':['Bank','Bank','GSE','PSE'],
            'Sub Cat':['Tier1','Small','Small', 'Small'],
            'Location':['US','US','UK','UK'],
            'Amount':[50, 55, 65, 55],
            'Amount1':[1,2,3,4]})

df2=df1.groupby(['Counterparty','Location'])[['Amount']].sum()
df2.dtypes
df1.dtypes

df2数据框没有我正在汇总的列(交易对手"和位置").任何想法为什么会是这种情况? Amount和Amount1都是数字字段.我只想汇总Amount,汇总Amount1

The df2 data frame does not have the columns that I am aggregating across ( Counterparty and Location). Any ideas why this is the case ? Both Amount and Amount1 are numeric fields. I just want to sum across Amount and aggregate across Amount1

推荐答案

对于索引中的列,请添加as_index=False参数或

For columns from index add as_index=False parameter or reset_index:

df2=df1.groupby(['Counterparty','Location'])[['Amount']].sum().reset_index()
print (df2)
  Counterparty Location  Amount
0         Bank       US     105
1          GSE       UK      65
2          PSE       UK      55

df2=df1.groupby(['Counterparty','Location'], as_index=False)[['Amount']].sum()
print (df2)
  Counterparty Location  Amount
0         Bank       US     105
1          GSE       UK      65
2          PSE       UK      55

如果此处按所有列进行汇总

If aggregate by all columns here happens automatic exclusion of nuisance columns - column Sub Cat is omitted:

df2=df1.groupby(['Counterparty','Location']).sum().reset_index()
print (df2)
  Counterparty Location  Amount  Amount1
0         Bank       US     105        3
1          GSE       UK      65        3
2          PSE       UK      55        4


df2=df1.groupby(['Counterparty','Location'], as_index=False).sum()

这篇关于 pandas groupby存储在一个新的数据框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆