How to Create a Graph From Dataframe in Python
To create a graph from a dataframe in Python, you can use the matplotlib library’s pyplot module.
The following example shows how to create a graph from a dataframe in Python using the pyplot module.
Using pyplot Module
We can use the pyplot module to create a graph from a dataframe.
Suppose we have the following dataframe:
# Import library
import pandas as pd
import matplotlib.pyplot as plt
# Define 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]
})
# Plot bar graph for dataframe
office_stuff.plot(x="Product_Name", y="Price", kind="bar")
plt.show()
Output: 👇️
In this example, we use the pyplot module to create a bar graph from the dataframe office_stuff.
The output shows the bar graph created using the pyplot module.
Conclusion
We can use the pyplot module from the matplotlib library to create a graph from a dataframe in Python.