Giả sử chúng ta có nội dung file CSV như sau:
Car Reg_Price 0 BMW 2000 1 Lexus 1500 2 Audi 1500 3 Jaguar 2000 4 Mustang 1500
Nhập các thư viện cần thiết:
import pandas as pd import matplotlib.pyplot as plt
File CSV nằm trên Desktop. Tải dữ liệu từ file CSV vào Pandas DataFrame:
d = pd.read_csv("C:\\Users\\amit_\\Desktop\\SalesData.csv")
dataFrame = pd.DataFrame(d.head(), columns=["Car","Reg_Price"])
Vẽ biểu đồ từ DataFrame:
dataFrame.plot(x="Car", y="Reg_Price", kind="bar", figsize=(10, 9))
Ví dụ hoàn chỉnh
Dưới đây là mã nguồn đầy đủ:
import pandas as pd
import matplotlib.pyplot as plt
# đọc file csv
d = pd.read_csv("C:\\Users\\amit_\\Desktop\\SalesData.csv")
print("\nĐang đọc file CSV...\n", d)
# tạo DataFrame
dataFrame = pd.DataFrame(d.head(), columns=["Car","Reg_Price"])
# vẽ biểu đồ cột
dataFrame.plot(x="Car", y="Reg_Price", kind="bar", figsize=(10, 9))
# hiển thị biểu đồ
plt.show()
Kết quả
Chương trình sẽ hiển thị biểu đồ cột như sau:
