renaming columns after group by and sum in pandas dataframe

前端 未结 2 1911
星月不相逢
星月不相逢 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'})
    
    0 讨论(0)
  • 2021-02-14 02:40
    df = df.groupby('col1')['col1'].count()
    df1= df.to_frame().rename(columns={'col1':'new_name'}).reset_index()
    
    0 讨论(0)
提交回复
热议问题