Home > Enterprise >  Pandas - Min and Max function
Pandas - Min and Max function

Time:06-18

I am playing with the min and max functions in python

I'm wondering if there's a way to get a unique list of items and then print the min and max values of each column

example:

Month T-shirt Sales Hat Sales short Sales
Jan 757 200 696
Feb 500 359 855
Mar 685 252 100
Jan 885 598 266
Feb 1001 223 1000
Mar 882 298 754

What I'd like to do here is find the min and max sales per month for each category

Desired Output

Month Max TS Sales Min TS Sales Max Hat Sales Min Hat Sales Max Short Sales Min short Sales
Jan 885 757 598 598 696 266
Feb 1001 500 223 359 1000 855
Mar 882 685 298 252 754 100

Is there a simple way to do this or is it a case that I'll have to get the min and max for each column separately and append to a DF?

Thanks

CodePudding user response:

import pandas as pd

df = pd.DataFrame({'month': ['Jan', 'Feb', 'Jan', 'Feb'],
                   't-shirt': [1, 2, 3, 4],
                   'hat': [4, 3, 2, 1]})

res = df.groupby(by='month').agg(['min', 'max'])
  • Related