Home > Mobile >  How to group DataFrame by two columns and create two new columns with min and max values from third
How to group DataFrame by two columns and create two new columns with min and max values from third

Time:01-23

For example, I have the DataFrame:

a = [{'column_1': 'A', 'column_2': 'B', 'column_3': 3},
     {'column_1': 'A', 'column_2': 'B', 'column_3': 8},
     {'column_1': 'Y', 'column_2': 'C', 'column_3': 2},
     {'column_1': 'Y', 'column_2': 'C', 'column_3': 10},
     {'column_1': 'Y', 'column_2': 'C', 'column_3': 6},
     {'column_1': 'F', 'column_2': 'S', 'column_3': 11},
     {'column_1': 'F', 'column_2': 'S', 'column_3': 18},
     {'column_1': 'F', 'column_2': 'S', 'column_3': 5},
     {'column_1': 'F', 'column_2': 'S', 'column_3': 20}]
df = pd.DataFrame(a)
print(df)

I need to group a DataFrame by columns df['column_1'] and df['column_2'], create two new columns df['min'] and df['max'], insert the min and max values from column df['column_3']

I need the next result:

enter image description here

CodePudding user response:

As per documentation:

df1 = df.groupby(['column_1', 'column_2'], as_index=False)['column_3'].agg({'min':'min', 'max':'max'})
  • Related