Pandas – Want to plot the top 20 Prices (float) for Sire (string)

Question:

Hi I’m new to Pandas so any help is much appreciated. I would like to show the top 20 performing Sires (string) by Price (float).

I am currently using

df.nlargest(n=20, columns=['Price'])

which gives me a list of the top 20 prices. I now want to plot this.

I currently have

df.groupby('Sire').sum().plot(y='Price', kind='bar')

but this gives me a list of all Sires rather than the top 20 by price.

any help is very much appreciated.

Asked By: Leonie

||

Answers:

Without knowing what the structure of your DataFrame looks like it is hard to help. You might just try doing both operations. Perhaps something like:

df.groupby('Sire').sum().nlargest(n=20, columns=['Price']).plot(y='Price', kind='bar')

or

df.nlargest(n=20, columns=['Price']).groupby('Sire').sum().plot(y='Price', kind='bar')
Answered By: Jeanot Zubler
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.