Home > other >  Trying to use df.groupby function to group new dataframe according to year information
Trying to use df.groupby function to group new dataframe according to year information

Time:12-01

I need to use the groupby function to group new dataframe according to year. I have seen other topics on this issue however they don't have it reading from a csv file. I'm wondering am I already doing this right or if I am wrong what is the right way to do this

I tried using

df = pd.read_csv('data.csv', usecols= ['price','year'])

df.groupby('price')
print(df)

But this gives me back information that is in the image -> groupby result

CodePudding user response:

You could do that in this way:

df = df.groupby('year')

Print first value in each group:

df.first()

to get highest price for each year group:

df.groupby('year').max()

Or:

 df.groupby('year')['price'].max()
  • Related