pandas dataframe groupby sum index

前端 未结 1 773
醉话见心
醉话见心 2021-01-15 08:04

I have a dataframe, I want to

    FROM:  
             dow    yield
           0    F   2
           1    F   3
           2    M   4
           3    M   6
         


        
相关标签:
1条回答
  • 2021-01-15 08:49

    First column is index, so you can add parameter as_index=False:

    df1 = df.groupby('dow', as_index=False).sum()
    print (df1)
      dow  yield
    0   F      5
    1   M     10
    2  TH      7
    

    Or reset_index:

    df1 = df.groupby('dow').sum().reset_index()
    print (df1)
      dow  yield
    0   F      5
    1   M     10
    2  TH      7
    
    0 讨论(0)
提交回复
热议问题