Table of Contents
A Pandas DataFrame is a data structure used for storing and manipulating tabular data in Python. To create a boxplot from a Pandas DataFrame, you can use the built-in plot function and specify the ‘kind’ parameter as ‘box’. This will generate a boxplot visualizing the distribution of the data within the DataFrame. Additionally, you can customize the appearance of the boxplot by specifying the ‘color’ and ‘whis’ (whisker length) parameters. By creating a boxplot from a Pandas DataFrame, you can easily identify outliers and the overall spread of the data.
Create Boxplot from Pandas DataFrame
You can use the following syntax to create boxplots from a pandas DataFrame:
#create boxplot of one column df.boxplot(column=['col1']) #create boxplot of multiple columns df.boxplot(column=['col1', 'col2']) #create boxplot grouped by one column df.boxplot(column=['col1'], by='col2')
The following examples show how to use this syntax in practice with the following DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'conference': ['A', 'A', 'A', 'B', 'B', 'B'], 'points': [5, 7, 7, 9, 12, 9], 'assists': [11, 8, 10, 6, 6, 5], 'rebounds': [4, 2, 5, 8, 6, 11],}) #view DataFrame df
Example 1: Boxplot of One Column
The following code shows how to create a boxplot for one column in a pandas DataFrame:
df.boxplot(column=['points'], grid=False, color='black')
Example 2: Boxplot of Multiple Columns
The following code shows how to create a boxplot for multiple columns in a pandas DataFrame:
df.boxplot(column=['points', 'assists'], grid=False, color='black')
Example 3: Boxplot Grouped by One Column
The following code shows how to create a boxplot grouped by one column in a pandas DataFrame:
df.boxplot(column=['points'], by='conference', grid=False, color='black')