Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading