Pandas: aggregating multiple columns with multiple functions

Viewed 2392

Pandas in Python and Dplyr in R are both flexible data wrangling tools. For example, in R, with dplyr one can do the following;

custom_func <- function(col1, col2) length(col1) + length(col2)

ChickWeight %>% 
  group_by(Diet) %>% 
  summarise(m_weight = mean(weight), 
            var_time = var(Time), 
            covar = cov(weight, Time),
            odd_stat = custom_func(weight, Time))

Notice how in one statement;

  • I can aggregate over multiple columns in one line.
  • I can apply different functions over these multiple columns in one line.
  • I can use functions that take into account two columns.
  • I can throw in custom functions for any of these.
  • I can declare new column names for these aggregations.

Is such a pattern also possible in pandas? Note that I am interested in doing this in a short statement (so not creating three different dataframes and then joining them).

3 Answers

I am bringing datar here for you to translate your R code into python:

>>> from pipda import register_func
>>> from datar.all import f, length, group_by, summarise, mean, var, cov
>>> from datar.datasets import ChickWeight
>>> 
>>> @register_func(None)
... def custom_func(col1, col2):
...     return length(col1) + length(col2)
... 
>>> ChickWeight >> \
...     group_by(f.Diet) >> \
...     summarise(
...         m_weight=mean(f.weight),
...         var_time = var(f.Time),
...         covar = cov(f.weight, f.Time),
...         odd_stat = custom_func(f.weight, f.Time)
...     )
     Diet    m_weight   var_time       covar  odd_stat
  <int64>   <float64>  <float64>   <float64>   <int64>
0       1  102.645455  45.422397  312.189871       440
1       2  122.616667  45.909722  398.564426       240
2       3  142.950000  45.909722  528.827731       240
3       4  135.262712  45.100618  441.868535       236

I am the author of the package. Feel free to submit issues if you have any questions.

Related