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;
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).
To apply aggregations to multiple columns, just add additional key:value pairs to the dictionary. Applying multiple aggregation functions to a single column will result in a multiindex. Working with multi-indexed columns is a pain and I'd recommend flattening this after aggregating by renaming the new columns.
pandas GroupBy Multiple Columns Example You can do so by passing a list of column names to DataFrame. groupby() function.
By use + operator simply you can combine/merge two or multiple text/string columns in pandas DataFrame. Note that when you apply + operator on numeric columns it actually does addition instead of concatenation.
Grouping by multiple columns with multiple aggregations functions. Can you groupby your data set multiple columns in Pandas? You bet! Here's an example of multiple aggregations per grouping, each with their specific calculated function: a sum of the aggregating column and an average calculation.
With pandas groupby.apply() you can run multiple functions in a groupby aggregation. Please note for statistical functions you would need scipy
installed. For custom functions will need to run an aggregate like sum()
for groupwise data:
def customfct(x,y):
data = x / y
return data.mean()
def f(row):
row['m_weight'] = row['weight'].mean()
row['var_time'] = row['Time'].var()
row['cov'] = row['weight'].cov(row['Time'])
row['odd_stat'] = customfct(row['weight'], row['Time'])
return row
aggdf = df.groupby('Diet').apply(f)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With