Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot frequency count of pandas column?

Tags:

python

pandas

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()
like image 795
BhishanPoudel Avatar asked Dec 28 '18 05:12

BhishanPoudel


People also ask

How do you count the frequency of a column in pandas?

In pandas you can get the count of the frequency of a value that occurs in a DataFrame column by using Series. value_counts() method, alternatively, If you have a SQL background you can also get using groupby() and count() method.

How do you count the frequency of a column?

Note: You also can use this formula =COUNTIF(A1:A10,"AAA-1") to count the frequency of a specific value. A1:A10 is the data range, and AAA-1 is the value you want to count, you can change them as you need, and with this formula, you just need to press Enter key to get the result.

How can you get the frequency of different levels in a categorical column in Python?

To create a frequency column for categorical variable in an R data frame, we can use the transform function by defining the length of categorical variable using ave function. The output will have the duplicated frequencies as one value in the categorical column is likely to be repeated.


1 Answers

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.

like image 79
jezrael Avatar answered Sep 30 '22 22:09

jezrael