How to aggregate multiple columns - Pandas

qwerty

I have this df:

ID         Date  XXX  123_Var  456_Var  789_Var  123_P  456_P  789_P
 A  07/16/2019     1      987      551      313     22     12     94
 A  07/16/2019     9      135      748      403     92     40     41
 A  07/18/2019     8      376      938      825     14     69     96
 A  07/18/2019     5      259      176      674     52     75     72
 B   07/16/2019    9      690      304      948     56     14     78
 B   07/16/2019    8      819      185      699     33     81     83
 B   07/18/2019    1      580      210      847     51     64     87

I want to group the df by ID and Date, aggregate the XXX column by the maximum value, and aggregate 123_Var, 456_Var, 789_Var columns by the minimum value.

* Note: The df contains many of these columns. The shape is: {some int}_Var.

This is the current code I've started to write:

df = (df.groupby(['ID','Date'], as_index=False)
        .agg({'XXX':'max', list(df.filter(regex='_Var')): 'min'}))

Expected result:

ID         Date  XXX  123_Var  456_Var  789_Var
 A  07/16/2019     9      135      551      313
 A  07/18/2019     8      259      176      674
 B   07/16/2019    9      690      185      699
 B   07/18/2019    1      580      210      847
jezrael

Create dictionary dynamic with dict.fromkeys and then merge it with {'XXX':'max'} dict and pass to GroupBy.agg:

d = dict.fromkeys(df.filter(regex='_Var').columns, 'min')
df = df.groupby(['ID','Date'], as_index=False).agg({**{'XXX':'max'}, **d})
print (df)
  ID        Date  XXX  123_Var  456_Var  789_Var
0  A  07/16/2019    9      135      551      313
1  A  07/18/2019    8      259      176      674
2  B  07/16/2019    9      690      185      699
3  B  07/18/2019    1      580      210      847

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to group by and aggregate on multiple columns in pandas

Aggregate unique values from multiple columns with pandas GroupBy

Python pandas groupby aggregate on multiple columns, then pivot

pandas aggregate function with multiple output columns

How to groupby() aggregate on multiple columns and rename the multi-index in Pandas 0.21+?

pandas dataframe resample aggregate function use multiple columns with a customized function?

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

pandas group by, aggregate using multiple agg functions on input columns

How to aggregate by day with multiple columns [Pyspark]?

pandas groupby aggregate customised function with multiple columns

how to choose multiple columns in aggregate functions?

How to aggregate multiple columns in a dataframe using values multiple columns

How to aggregate only some columns horizontally in Pandas

Pandas groupby aggregate apply multiple functions to multiple columns

How to aggregate columns in pandas

How to aggregate multiple columns in pandas groupby

How can I aggregate on multiple columns in pandas?

Aggregate Pandas DataFrame based on condition that uses multiple columns?

how to aggregate multiple columns of a dataframe with dplyr

pandas groupby + multiple aggregate/apply with multiple columns

How do you aggregate multiple columns from large DataFrame using pandas?

Pandas: How to aggregate multiple columns into value counts, generating new columns for each value count?

Create multiple columns with calculations using groupby, aggregate functions in Pandas

Pandas dataframe, groupBy aggregate multiple columns and rows

Group and aggregate by multiple columns using Pandas NamedAgg

Pandas - aggregate multiple columns with pivot_table

How to Aggregate data based on multiple columns in Python

Pandas: How to aggregate a column with multiple functions and add the results as other columns?

pandas aggregate multiple columns during pivot_table