2017-10-05 85 views
0

我的數據框如何刪除行數據

enter image description here

我想刪除同一行數據時col["SALE PRICE"] = '-'

import pandas as pd 
import numpy as np 

sales = pd.read_csv('C:\\Users\\Jerry\\Downloads\\nyc-rolling-sales.csv',sep = ',',encoding = 'latin-1') 

del sales['BOROUGH'] 
del sales['NEIGHBORHOOD'] 
del sales['BLOCK'] 
del sales['ADDRESS'] 
del sales['APARTMENT NUMBER'] 

sales['SALE PRICE'] = np.where(sales['SALE PRICE']=' - ',delimeter, sales['SALE PRICE']) 

語法錯誤:關鍵字不能表達

回答

1

你是在以下行上檢查與=而不是==的等同性

sales['SALE PRICE'] = np.where(sales['SALE PRICE']=' - ',delimeter, sales['SALE PRICE']) 

請改爲此

sales['SALE PRICE'] = np.where(sales['SALE PRICE']==' - ',delimeter, sales['SALE PRICE']) 
0

更新:全碼

如何:

import pandas as pd 
import numpy as np 

file = 'C:\\Users\\Jerry\\Downloads\\nyc-rolling-sales.csv' 
sales = pd.read_csv(file,sep = ',',encoding = 'latin-1') 

dropcols = ["BOROUGH","NEIGHBORHOOD","BLOCK","ADDRESS","APARTMENT NUMBER"] 
sales = sales.drop(dropcols,axis=1) 

#boolean indexing 
mask = sales['SALE PRICE'] == "-" 
#mask = sales['SALE PRICE'].str.strip() == "-" #use this for spaces 
sales[~mask] 

我不知道你在哪裏得到的數據,但你也可以插入一個直接鏈接:

例如

url = "http://www1.nyc.gov/assets/finance/downloads/pdf/rolling_sales/rollingsales_manhattan.xls" 
sales = pd.read_excel(url,skiprows=4)