Python - 如何将一个 Pandas 数据帧绘制成条形图
假设以下内容是 CSV 文件的内容 -
Car Reg_Price 0 BMW 2000 1 Lexus 1500 2 Audi 1500 3 Jaguar 2000 4 Mustang 1500
导入所需的库 -
import pandas as pd import matplotlib.pyplot as mp
我们的 CSV 文件在桌面上。将 CSV 文件中的数据加载到 Pandas 数据帧中 -
d = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv") dataFrame = pd.DataFrame(d.head(), columns=["Car","Reg_Price"])
绘制数据帧 -
dataFrame.plot(x="Car", y="Reg_Price", kind="bar", figsize=(10, 9))
示例
以下是代码 -
import pandas as pd import matplotlib.pyplot as mp # read csv d = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv") print("\nReading the CSV file...\n",d) # dataframe dataFrame = pd.DataFrame(d.head(), columns=["Car","Reg_Price"]) # plotting the dataframe dataFrame.plot(x="Car", y="Reg_Price", kind="bar", figsize=(10, 9)) # displaying bar graph mp.show()
输出
将显示以下输出 -
广告