Đọc products.csv, in số dòng, cột. Sau đó in 10 dòng đầu cột 'product' khớp 'Car'.
Kết quả mong đợi:
Rows: 100 Columns: 8 id product engine avgmileage price height_mm width_mm productionYear 1 2 Car Diesel 21 16500 1530 1735 2020 4 5 Car Gas 18 17450 1530 1780 2018 5 6 Car Gas 19 15250 1530 1790 2019 8 9 Car Diesel 23 16925 1530 1800 2018
Giải pháp 1
- Đọc CSV:
df = pd.read_csv('products.csv') - In shape:
df.shape[0]rows,df.shape[1]columns - Lọc 10 dòng đầu:
df1 = df.iloc[0:10,:] - Lọc product=='Car':
df1[df1.iloc[:,1]=='Car'](cột product index=1)
Code
import pandas as pd
df = pd.read_csv('products.csv')
print("Rows:",df.shape[0],"Columns:",df.shape[1])
df1 = df.iloc[0:10,:]
print(df1[df1.iloc[:,1]=='Car'])Kết quả
Rows: 100 Columns: 8 id product engine avgmileage price height_mm width_mm productionYear 1 2 Car Diesel 21 16500 1530 1735 2020 4 5 Car Gas 18 17450 1530 1780 2018 5 6 Car Gas 19 15250 1530 1790 2019 8 9 Car Diesel 23 16925 1530 1800 2018
Giải pháp 2
- Đọc CSV, in shape.
- Lấy 10 dòng đầu:
df1 = df.head(10) - Lọc product=='Car':
df1[df1['product']=='Car']
Code
import pandas as pd
df = pd.read_csv('products.csv')
print("Rows:",df.shape[0],"Columns:",df.shape[1])
df1 = df.head(10)
print(df1[df1['product']=='Car'])Kết quả
Rows: 100 Columns: 8 id product engine avgmileage price height_mm width_mm productionYear 1 2 Car Diesel 21 16500 1530 1735 2020 4 5 Car Gas 18 17450 1530 1780 2018 5 6 Car Gas 19 15250 1530 1790 2019 8 9 Car Diesel 23 16925 1530 1800 2018