Sum up column values in Pandas DataFrame

前端 未结 1 602
无人及你
无人及你 2020-12-16 13:49

In a pandas DataFrame, is it possible to collapse columns which have identical values, and sum up the values in another column?

Code



        
相关标签:
1条回答
  • This is a job for groupby:

    >>> df.groupby(["score", "type"]).sum()
                            count
    score    type                
    9.397000 advanced  537.331573
    9.397995 advanced    9.641728
    9.397996 newbie      0.100000
    9.399900 expert     19.6541374
    >>> df.groupby(["score", "type"], as_index=False).sum()
          score      type       count
    0  9.397000  advanced  537.331573
    1  9.397995  advanced    9.641728
    2  9.397996    newbie    0.100000
    3  9.399900    expert   19.654137
    
    0 讨论(0)
提交回复
热议问题