สมมติว่าต่อไปนี้คือเนื้อหาของไฟล์ 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 DataFrame -
d = pd.read_csv("C:\\Users\\amit_\\Desktop\\SalesData.csv")
dataFrame = pd.DataFrame(d.head(), columns=["Car","Reg_Price"]) พล็อต DataFrame -
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() ผลลัพธ์
ซึ่งจะแสดงผลลัพธ์ต่อไปนี้ -
