How to create a histogram from a dataframe using pandas in python ?

Published: December 17, 2020

Tags: Python; Pandas; DataFrame;

DMCA.com Protection Status

Examples of how to create a histogram from a dataframe using pandas in python

Create a dataframe with pandas

To start, lets create a simple dataframe with pandas:

import pandas as pd
import matplotlib.pyplot as plt

data = {'c':['a','a','a','b','b','b','a','a','b'], 
        'v1':[1,1,2,3,4,4,4,5,5],
        'v2':[6,6,4,4,4,5,5,7,8]}

df = pd.DataFrame(data)

print(df)

returns

   c  v1  v2
0  a   1   6
1  a   1   6
2  a   2   4
3  b   3   4
4  b   4   4
5  b   4   5
6  a   4   5
7  a   5   7
8  b   5   8

Create histograms

To create a histogram from a given column:

hist = df['v1'].hist()

plt.savefig("pandas_hist_01.png", bbox_inches='tight', dpi=100)

How to create an histogram from a dataframe using pandas in python ?
How to create an histogram from a dataframe using pandas in python ?

To create a histogram from a given column and create groups using another column:

hist = df['v1'].hist(by=df['c'])

plt.savefig("pandas_hist_02.png", bbox_inches='tight', dpi=100)

How to create an histogram from a dataframe using pandas in python ?
How to create an histogram from a dataframe using pandas in python ?

To create two histograms from columns v1 and v2

df.hist(column=['v1','v2'])

plt.savefig("pandas_hist_03.png", bbox_inches='tight', dpi=100)

How to create an histogram from a dataframe using pandas in python ?
How to create an histogram from a dataframe using pandas in python ?

Same result

hist = df[['v1','v2']].hist()

plt.savefig("pandas_hist_04.png", bbox_inches='tight', dpi=100)

How to create an histogram from a dataframe using pandas in python ?
How to create an histogram from a dataframe using pandas in python ?

To create two histograms that share the same x and y axes:

hist = df[['v1','v2']].hist(sharey=True, sharex=True)

plt.savefig("pandas_hist_05.png", bbox_inches='tight', dpi=100)

How to create an histogram from a dataframe using pandas in python ?
How to create an histogram from a dataframe using pandas in python ?

hist = df.hist(column=['v1'], by=df['c'])

plt.savefig("pandas_hist_06.png", bbox_inches='tight', dpi=100)

How to create an histogram from a dataframe using pandas in python ?
How to create an histogram from a dataframe using pandas in python ?

Image

of