How to visualize data using pandas

Published on Aug. 22, 2023, 12:17 p.m.

You can visualize data using pandas by using the plot() function, which provides a range of different plots, including line plots, bar plots, histograms, scatter plots, and more. Here’s an example of how to create a line plot of data from a pandas DataFrame:

import pandas as pd
import matplotlib.pyplot as plt

# create a DataFrame with some sample data
data = {'year': [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020],
        'sales': [10, 13, 15, 20, 25, 30, 35, 42, 50, 60, 75]}
df = pd.DataFrame(data)

# create a line plot of the data
df.plot(x='year', y='sales', kind='line')
plt.show()

This will create a line plot of the ‘year’ column on the x-axis and the ‘sales’ column on the y-axis.

You can also create other types of plots by changing the ‘kind’ parameter, such as a bar plot, a histogram, or a scatter plot:

# create a bar plot
df.plot(x='year', y='sales', kind='bar')
plt.show()

# create a histogram
df.plot(y='sales', kind='hist')
plt.show()

# create a scatter plot
df.plot(x='year', y='sales', kind='scatter')
plt.show()

In addition to the plot() function, pandas also provides other visualization functions, such as boxplot() and hist() for creating box plots and histograms, respectively. You can refer to the pandas documentation for more information on all the available visualization functions.