+3 votes
in Programming Languages by (51.8k points)
What is the Pythonic way to generate a histogram using the data of each column of a Pandas dataframe?

1 Answer

+2 votes
by (66.2k points)
selected by
Best answer

The hist() function of Pandas dataframe generates a histogram for each column. If the dataframe has a small number of columns, you can use this function.

Here is an example:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# create a dataframe with random numbers
df = pd.DataFrame(np.random.randn(100, 3), columns=['A', 'B', 'C'])
bp = df.hist(bins=10)

The above code will generate the following plot:

histogram using pandas dataframe