0

Lets say i have the following code which create random numbers just for the example. Getting the group by is ok as long as it does only one action per each column. So df is grouped by C and gets column A max value and column B gets it's min.

I am asking how I can get for column A both max and min (I assume this will have to create new columns?

Your help is appreciated!

import numpy as np
import pandas as pd


df = pd.DataFrame(np.random.randint(0,5,size=(20, 3)), columns=list('ABC'))
print(df)

print(df.groupby('C').agg({'A': max, 'B':min}))
#or can use this
#print(df.agg({'A': np.max,'B': np.min,}).head(10))
Giladbi
  • 1,822
  • 3
  • 19
  • 34

0 Answers0