Skip to content
Advertisement

Can pandas perform an aggregating operation involving two columns?

Given the following dataframe,
is it possible to calculate the sum of col2 and the sum of col2 + col3,
in a single aggregating function?

JavaScript
. col1 col2 col3
0 a 1 10
1 a 2 20
2 b 3 30
3 b 4 40

In R’s dplyr I would do it with a single line of summarize,
and I was wondering what might be the equivalent in pandas:

JavaScript

Desired result:

. col1 col2_sum col23_sum
0 a 3 33
1 b 7 77

Advertisement

Answer

Let us try assign the new column first

JavaScript

Out[81]:

JavaScript

From my understanding the apply is more like the summarize in R

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement