Home > Enterprise >  how to groupby and sum multiple columns in pandas without listing them all
how to groupby and sum multiple columns in pandas without listing them all

Time:07-25

Background

I've got a dataframe with 100 columns, basically ['Year', 'month', 'a', 'b', 'c'...]

Year    Month   a   b   c   d   e
2012    Jan    74   67  22  51  90
2012    Feb    100  2   46  75  1
2013    Mar    47   38  52  91  63
2013    Apr    52   83  34  59  44

Problem

I want to group by year and sum all the values, but I don't know how to do it without listing them all. like below;

          a  b   c    d   e
2012    174  69 68  126  91
2013     99 121 86  150 107

What I've tried

I've converted my columns to a list, and tried to feed that into the groupby function, but no luck

headers = list(df.iloc[:, 2:100].columns)
print(headers)

sum_df = df.groupby(['Year']).sum(headers)

Help requested

Anyone know a easy way to do this?

CodePudding user response:

You can use select_dtypes:

>>> df.select_dtypes('number').groupby('Year', as_index=False).sum()

   Year    a    b   c    d    e
0  2012  174   69  68  126   91
1  2013   99  121  86  150  107
  • Related