20

I have a pandas dataframe like this:

    Year   Winner
4   1954  Germany
9   1974  Germany
13  1990  Germany
19  2014  Germany
5   1958   Brazil
6   1962   Brazil
8   1970   Brazil
14  1994   Brazil
16  2002   Brazil

How to plot the frequency count of column Winner, so that y axis has frequency and x-axis has name of country?

I tried:

import numpy as np
import pandas as pd

df.groupby('Winner').size().plot.hist()
df1['Winner'].value_counts().plot.hist()

3 Answers 3

38

You are close, need Series.plot.bar because value_counts already count frequency:

df1['Winner'].value_counts().plot.bar()

g

Also working:

df1.groupby('Winner').size().plot.bar()

Difference between solutions is output of value_counts will be in descending order so that the first element is the most frequently-occurring element.

Sign up to request clarification or add additional context in comments.

1 Comment

How would you get the relative frequency for the same plot?
9

In addition to @jezrael's answer, you can also do:

df1['Winner'].value_counts().plot(kind='bar')

Other one from @jezrael could be:

df1.groupby('Winner').size().plot(kind='bar')

Comments

0

Just want to say this works with the latest version of plotly. Just need to add ,text_auto=True. Example: px.histogram(df, x="User Country",text_auto=True)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.