我有一個包含超過 5,000,000 行資料的 cvs 檔案,看起來像這樣(除了它是波斯語):
Contract Code,Contract Type,State,City,Property Type,Region,Usage Type,Area,Percentage,Price,Price per m2,Age,Frame Type,Contract Date,Postal Code
765720,Mobayee,East Azar,Kish,Apartment,,Residential,96,100,570000,5937.5,36,Metal,13890107,5169614658
766134,Mobayee,East Azar,Qeshm,Apartment,,Residential,144.5,100,1070000,7404.84,5,Concrete,13890108,5166884645
766140,Mobayee,East Azar,Tabriz,Apartment,,Residential,144.5,100,1050000,7266.44,5,Concrete,13890108,5166884645
766146,Mobayee,East Azar,Tabriz,Apartment,,Residential,144.5,100,700000,4844.29,5,Concrete,13890108,5166884645
766147,Mobayee,East Azar,Kish,Apartment,,Residential,144.5,100,1625000,11245.67,5,Concrete,13890108,5166884645
770822,Mobayee,East Azar,Tabriz,Apartment,,Residential,144.5,50,500000,1730.1,5,Concrete,13890114,5166884645
我想撰寫一個代碼將第一行作為標題傳遞,然后從兩個特定城市(Kish 和 Qeshm)中提取資料并將其保存到新的 cvs 檔案中。像這樣的東西:
Contract Code,Contract Type,State,City,Property Type,Region,Usage Type,Area,Percentage,Price,Price per m2,Age,Frame Type,Contract Date,Postal Code
765720,Mobayee,East Azar,Kish,Apartment,,Residential,96,100,570000,5937.5,36,Metal,13890107,5169614658
766134,Mobayee,East Azar,Qeshm,Apartment,,Residential,144.5,100,1070000,7404.84,5,Concrete,13890108,5166884645
766147,Mobayee,East Azar,Kish,Apartment,,Residential,144.5,100,1625000,11245.67,5,Concrete,13890108,5166884645
值得一提的是,我對 python 很陌生。我撰寫了以下塊來定義標題,但這是迄今為止我得到的最遠的。
import pandas as pd
path = '/Users/Desktop/sample.csv'
df = pd.read_csv(path , header=[0])
df.head = ()
uj5u.com熱心網友回復:
不需要使用header=...,因為默認是把第一行當做表頭,所以
df = pd.read_csv(path)
然后,保持行的條件:
df2 = df[df['City'].isin(['Kish', 'Qeshm'])]
你可以保存它
df2.to_csv(another_path)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/429194.html
標籤:Python python-3.x 熊猫 蜘蛛 简历
