How to Create Bar Chart From Dataframe in Python
To create a bar chart from a dataframe in Python, you can use the plot() function from the pandas library.
The following example shows how to create a bar chart from a dataframe in Python using the plot() function.
Using plot() Function
We can use the plot() function to create a bar chart 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
# Load the data into a dataframe
office_stuff = pd.DataFrame({
'Product_Name': ['Laptop', 'Mobile', 'Printer', 'Keyboard', 'Scanner', 'Mouse'],
'Price': [4500, 550, 250, 50, 350, 50]
})
# Create a bar chart
office_stuff.plot(x='Product_Name', y='Price', kind='bar')
plt.title('Product Prices')
plt.xlabel('Product Name')
plt.ylabel('Price')
plt.show()
Output: 👇️
In this example, we use the plot() function to create a bar chart from the dataframe office_stuff.
The kind=‘bar’ parameter specifies that we want to create a bar chart. The output shows the bar chart created from the dataframe.
Conclusion
We can use the plot() function from the pandas library to create a bar chart from a dataframe in Python.