pandas.DataFrame.hist() Method

Learn about the pandas.DataFrame.hist() method with its usages, syntax, and example. By Pranit Sharma Last updated : October 06, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Python DataFrame.hist() Method

The pandas.DataFrame.hist() method is useful for distributing the numerical values. This method decomposes different types of values into numerical values. Its main functionality is to make the Histogram of a given Data frame.

The histogram represents the distribution of data. When DataFrame.hist() method is used, it automatically calls the method matplotlib.pyplot.hist() on each series in the DataFrame. This results in one histogram per column.

Syntax

The syntax of pandas.DataFrame.hist() method is:

DataFrame.hist(
    column=None, 
    by=None, 
    grid=True, 
    xlabelsize=None, 
    xrot=None, 
    ylabelsize=None, 
    yrot=None, 
    ax=None, 
    sharex=False, 
    sharey=False, 
    figsize=None, 
    layout=None, 
    bins=10, 
    backend=None, 
    legend=False, 
    **kwargs
    )

Parameter(s)

The parameters of pandas.DataFrame.hist() method are:

  • data: DataFrame
  • column: sequence
  • xlabelsize: int, default None, represents the plot on x axis
  • ylabelsize: int, default None, represent the plot on y axis
  • More [source]

Python example of pandas.DataFrame.hist() Method

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating a dictionary
d = {
    'Length': [2.7, 8.7, 3.4, 2.4, 1.9],
    'Breadth': [4.24, 2.67, 7.6, 7.1, 4.9],
    'foo':['bar','zoo','foo','bar','foo']
}

# Creating a DataFrame
df = pd.DataFrame(d)

# Display original DataFrame
print("Original DataFrame:\n",df,"\n")

# Create histogram
hist = df.hist(bins=5)

# Display result
print("Histogram:\n",hist)

Output

The output of the above program is:

Example: pandas.DataFrame.hist() Method

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.