Pandas - dataframe groupby - how to get sum of multiple columns

Axel :

This should be an easy one, but somehow I couldn't find a solution that works.

I have a pandas dataframe which looks like this:

index col1   col2   col3   col4   col5
0     a      c      1      2      f 
1     a      c      1      2      f
2     a      d      1      2      f
3     b      d      1      2      g
4     b      e      1      2      g
5     b      e      1      2      g

I want to group by col1 and col2 and get the sum() of col3 and col4. Col5 can be dropped, since the data can not be aggregated.

Here is how the output should look like. I am interested in having both col3 and col4 in the resulting dataframe. It doesn't really matter if col1 and col2 are part of the index or not.

index col1   col2   col3   col4   
0     a      c      2      4          
1     a      d      1      2      
2     b      d      1      2      
3     b      e      2      4      

Here is what I tried:

df_new = df.groupby(['col1', 'col2'])["col3", "col4"].sum()

That however only returns the aggregated results of col4.

I am lost here. Every example I found only aggregates one column, where the issue obviously doesn't occur.

YOBEN_S :

By using apply

df.groupby(['col1', 'col2'])["col3", "col4"].apply(lambda x : x.astype(int).sum())
Out[1257]: 
           col3  col4
col1 col2            
a    c        2     4
     d        1     2
b    d        1     2
     e        2     4

If you want to agg

df.groupby(['col1', 'col2']).agg({'col3':'sum','col4':'sum'})

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Pandas Dataframe Groupby with multiple columns and sum it

Pandas - How to get get sum of rows by multiple columns in a DataFrame

Pandas groupby and get nunique of multiple columns in a dataframe

How to groupby multiple columns to list in pandas dataframe

Pandas Dataframe Groupby multiple columns

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

Incorrect Sum by Groupby Dataframe on multiple columns

Pandas groupby and weighted sum for multiple columns

Pandas - groupby, aggregate and scale on the sum of multiple columns

Sum based on multiple columns with pandas groupby

Python Pandas Dataframe - how to groupby year and summarize multiple columns in a table

How do I groupby multiple columns in a pandas dataframe?

How to get unique values from multiple columns in a pandas groupby

Pandas map groupby with multiple columns in same dataframe

Split dataframe based on multiple columns pandas groupby

Pandas dataframe, groupBy aggregate multiple columns and rows

how to sum across many columns with pandas groupby?

Pandas Groupby Sum to Columns

Pandas DataFrame Groupby two columns and get counts

Pandas DataFrame Groupby two columns and get counts

Groupby multiple columns but ignore orders and sum up other columns in Pandas

Pandas groupby get row with max in multiple columns

How to get a dataframe with the sum of the max values in a groupby?

Pandas Dataframe groupby two columns and sum up a column

groupby 1 column and sum of other columns as new dataframe pandas

How to Groupby columns(ignore order) in Pandas DataFrame?

How to GroupBy a Dataframe in Pandas and keep Columns

How to include columns in groupby that is not present in Pandas DataFrame

How to get unique information from multiple columns of a pandas dataframe?