![](/img/trans.png)
[英]Change datatype of columns in Pandas Dataframe depending on the original data type of the column
[英]Pandas - Can't change datatype of dataframe columns
從這里下載一些數據: http://insideairbnb.com/get-the-data.html
然后
listings = pd.read_csv('listings.csv')
試圖改變類型
listings.bathrooms = listings.bathrooms.astype('int64',errors='ignore')
listings.bedrooms = listings.bedrooms.astype('int64',errors='ignore')
listings.beds = listings.beds.astype('int64',errors='ignore')
listings.price = listings.price.replace('[\$,]','',regex=True).astype('float')
listings.price = listings.price.astype('int64',errors='ignore')
嘗試了其他一些組合,但最后會彈出錯誤或只是不更改數據類型。
編輯:更正了一些錯別字
最后一行中的撇號不在正確的位置,最后一個的類型也不正確:您需要 ' 而不是 `(可能是因為代碼塊而意外添加的)。
所以對我來說它是這樣工作的:
listings.price.astype('int64', errors='ignore')
但是,如果您想將其重新分配給原始變量,那么您需要與前幾行中使用的結構相同:
listings.price = listings.price.astype('int64', errors='ignore')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.