Chọn tập con hàng dùng điều kiện lọc.
File CSV SalesData.csv.
Tải dữ liệu:
dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\SalesData.csv") Hàng có Units > 100:
dataFrame[dataFrame["Units"] > 100]
Hàng có Reg_Price < 3000:
dataFrame[dataFrame["Reg_Price"] < 3000]
Ví dụ
import pandas as pd
dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\SalesData.csv")
print("\nĐọc file CSV...\n", dataFrame)
res2 = dataFrame[['Reg_Price', 'Units']]
print("\n2 cột:\n", res2)
print("\nXe Units > 100:\n", dataFrame[dataFrame["Units"] > 100])
print("\nXe Reg_Price < 3000:\n", dataFrame[dataFrame["Reg_Price"] < 3000]) Kết quả
Reading the CSV file... Car Reg_Price Units 0 BMW 2500 100 1 Lexus 3500 80 2 Audi 2500 120 3 Jaguar 2000 70 4 Mustang 2500 110 Displaying only one column Car: Reg_Price Units 0 2500 100 1 3500 80 2 2500 120 3 2000 70 4 2500 110 Name: Car, dtype: object Select cars with Units more than 100: Car Reg_Price Units 2 Audi 2500 120 4 Mustang 2500 110 Select cars with Reg_Price less than 3000: Car Reg_Price Units 0 BMW 2500 100 2 Audi 2500 120 3 Jaguar 2000 70 4 Mustang 2500 110