pandas collapsing row data

后端 未结 2 624
温柔的废话
温柔的废话 2021-01-26 11:28

New to pandas, I m climbing my learning curve here. I have a dataframe that looks like:

Relinquished Degree Count DTD DNP outindefinitely seasonout 
------------         


        
相关标签:
2条回答
  • 2021-01-26 12:18
    df.groupby('Relinquished').sum()
    
    0 讨论(0)
  • 2021-01-26 12:18

    You cannot replace missing values to empty strings in first step and then aggregate sum:

    df = df.groupby('Relinquished').sum()
    

    If want specify columns for sum:

    print (df)
      Relinquished  Degree  Count  DTD  DNP  outindefinitely  seasonout
    0      player1       1      1  1.0  NaN              NaN        NaN
    1      player1       3      1  NaN  NaN              1.0        NaN
    
    df = df.groupby('Relinquished')['DTD','DNP','outindefinitely','seasonout'].sum()
    print (df)
                  DTD  DNP  outindefinitely  seasonout
    Relinquished                                      
    player1       1.0  0.0              1.0        0.0
    
    0 讨论(0)
提交回复
热议问题