How to Create Boxplot From Dataframe in Python

To create a boxplot from a dataframe in Python, you can use the plot() function with the kind attribute set to box.

The following example shows how to create a boxplot from a dataframe in Python using the plot() function.

Using plot() Function

We can use the plot() function to create a boxplot from a dataframe.

Let’s see how to use the plot() function in Python:

# Import libraries
import pandas as pd
import matplotlib.pyplot as plt

# Create dataframe
Office_Stuff = pd.DataFrame({
    'Date': ['01-03-2023', '01-03-2023', '01-03-2023', '01-03-2023', '02-03-2023', '02-03-2023'],
    'Product_Code': ['A-101', 'A-102', 'A-103', 'B-101', 'B-102', 'B-104'],
    'Product_Name': ['Laptop', 'Mobile', 'Printer', 'Keyboard', 'Scanner', 'Mouse'],
    'Price': [4500, 550, 250, 50, 350, 50],
    'Status': [1, 1, 1, 0, 1, 1]
})

# Create boxplot
Office_Stuff.plot(x='Product_Name', y='Price', kind='box')

# Show boxplot
plt.show()

Output: 👇️

Box Plot

In this example, we use the plot() function to create a boxplot from the dataframe Office_Stuff.

The kind=‘box’ parameter specifies that we want to create a boxplot.

The output shows the boxplot created from the dataframe.

Conclusion

We can use the plot() function from the pandas library to create a boxplot from a dataframe in Python.