pandas 在pandas数据框中按分组和求和后重命名列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44416287/
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-08 15:48:14 来源:igfitidea点击:
renaming columns after group by and sum in pandas dataframe
提问by LKA
This is my group by command:
这是我的命令组:
pdf_chart_data1 = pdf_chart_data.groupby('sell').value.agg(['sum']).rename(
columns={'sum':'valuesum','sell' : 'selltime'}
)
I am able to change the column name for value but not for 'sell'. Please help to resolve this issue.
我可以更改 value 的列名,但不能更改“sell”的列名。请帮助解决此问题。
回答by jezrael
You cannot rename it, because it is index
. You can add as_index=False
for return DataFrame
or add reset_index
:
您不能重命名它,因为它是index
. 您可以添加as_index=False
返回DataFrame
或添加reset_index
:
pdf_chart_data1=pdf_chart_data.groupby('sell', as_index=False)['value'].sum()
.rename(columns={'sum':'valuesum','sell' : 'selltime'})
Or:
或者:
pdf_chart_data1=pdf_chart_data.groupby('sell')['value'].sum()
.reset_index()
.rename(columns={'sum':'valuesum','sell' : 'selltime'})