[英]Having difficulty dropping multiple columns from a Pandas dataframe
我有一個新創建的Pandas數據框,由於某種原因我不能刪除列。
RangeIndex:2617個條目,0到2616列:111個條目,對分區dtype的訪問:object(111)內存使用量:2.2+ MB無
print list(df)
['access','age','attic','basement_type','bathrooms','bedrooms_total','buyer','buyer_city','buyer_country','buyer_postal_code','colisting_agent_id','construction',' csa_number”,“ date_expired_hidden”,“ date_listed”,“ date_sold”,“ date_unconditional”,“ distance_to_schools”,“ distance_to_transportation”,“ district”,“ ensuites”,“ exterior_features”,“ exterior_finish”,“ fireplace_s” ,“地板”,“燃料”,“加熱”,“ id”,“ interior_features”,“ internet_remarks”]
df = df.drop(['buyer', 'buyer_city', 'buyer_country', 'colisting_agent_id'], axis=1, inplace=True)
print list(df)```
TypeErrorTraceback (most recent call last)
<ipython-input-63-1db0e7488634> in <module>()
6
7
----> 8 print list(df)
TypeError: 'NoneType' object is not iterable
您應該這樣做:
df = df.drop(['buyer', 'buyer_city', 'buyer_country'], axis=1)
要么:
df.drop(['buyer', 'buyer_city', 'buyer_country'], axis=1, inplace=True)
inplace=True
參數將放在源(即數據幀本身)處,並返回None
。
因此,當您使用inplace=True
請勿將其分配回df
這里的錯誤是因為df變量分配給None
並且在None
上調用list
構造函數將引發錯誤。
>>> df.drop(list_of_cols_to_drop, axis=1, inplace=True)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.