Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Sum rows in Pandas dataframe which match a column value?

I have a Pandas dataframe with ~22.000 rows. It contains 200 different scenarios (IDScen). I want to sum all the rows in mayjul and aug column for each scenario. Preferably it can come as a row beneath each scenario, with the sum.

IDScen  IDCell  Ctot    Sjul        Saug        mayjul      aug
1   37276   37.23220    0.001109    0.000057    0.000222    0.000011
1   36430   6.26848     0.004882    0.000109    0.000976    0.000022
1   37685   14.06670    0.000296    0.000007    0.000059    0.000001
1   38322   28.67790    0.000287    0.000010    0.000057    0.000002
1   36638   15.78980    0.002024    0.000056    0.000405    0.000011

So for example

    IDScen  IDCell  Ctot    Sjul        Saug        mayjul      aug
        1   37276   37.23220    0.001109    0.000057    0.000222    0.000011
        1   36430   6.26848     0.004882    0.000109    0.000976    0.000022
        1   37685   14.06670    0.000296    0.000007    0.000059    0.000001
        1   38322   28.67790    0.000287    0.000010    0.000057    0.000002
        1   36638   15.78980    0.002024    0.000056    0.000405    0.000011
sum scenario 1                                             sum         sum

I have tried to use the groupby function, but I can't seem to get it right

like image 720
joddm Avatar asked Jan 20 '26 08:01

joddm


1 Answers

df.groupby('IDScen')['mayjul', 'aug'].agg('sum')
like image 190
David Avatar answered Jan 22 '26 23:01

David