pandas dataframe sum with groupby

后端 未结 1 1787
暖寄归人
暖寄归人 2021-01-25 16:17

I have a pandas dataframe that structurally looks like this:

[
    [\'x\', \'1\', \'-7\']
    [\'x\', \'2\', \'-2\']
    [\'y\', \'3\', \'-1\']
    [\'y\', \'4\'         


        
相关标签:
1条回答
  • 2021-01-25 17:20

    setup
    I converted your string numbers to actual numbers

    df = pd.DataFrame(
        [
            ['x', '1', '-7'],
            ['x', '2', '-2'],
            ['y', '3', '-1'],
            ['y', '4', '-3']
        ]
    )
    
    df[1] = pd.to_numeric(df[1])
    df[2] = pd.to_numeric(df[2])
    

    solution

    df.groupby(0).sum()
    

    0 讨论(0)
提交回复
热议问题