renaming columns after group by and sum in pandas dataframe

前端 未结 2 1914
星月不相逢
星月不相逢 2021-02-14 01:49

This is my group by command:

pdf_chart_data1 = pdf_chart_data.groupby(\'sell\').value.agg([\'sum\']).rename(
    columns={\'sum\':\'valuesum\',\'sell\' : \'sellt         


        
2条回答
  •  北恋
    北恋 (楼主)
    2021-02-14 02:39

    You cannot rename it, because it is index. You can add as_index=False for return DataFrame or add 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'})
    

提交回复
热议问题