簡體   English   中英

pandas 未將 object dtype 轉換為 float64,即使在 df.astype('float64') 無錯誤執行后也是如此

[英]pandas not converting an object dtype to float64 even after error free execution of df.astype('float64')

我嘗試使用.astype('float64')將 object dtype 列轉換為 float64 它運行時沒有引發任何錯誤,但是當我使用 .dtype 或 .dtypes 檢查 dtype 時,它再次顯示轉換后的列為 object。

 real_estate['Age at time of purchase'].astype('float64')
164    67.0
153    61.0
133    56.0
132    56.0
179     NaN
       ... 
110    49.0
89     44.0
45     37.0
131    55.0
116    51.0
Name: Age at time of purchase, Length: 195, dtype: float64

real_estate.dtypes

Name                        object
Surname                     object
Age at time of purchase     object
Interval                    object
Y                          float64
M                          float64
D                          float64
Gender                      object
Country                     object
State                       object
dtype: object

為什么它不轉換,為什么不給出任何錯誤?

此外, real_estate['Age at time of purchase'].dtype這給了我一些我沒想到的東西。 dtype('O') dtype('O')是什么意思?

你可以這樣試試

real_estate['Age at time of purchase']=real_estate['Age at time of purchase'].astype('float64')

在我的一個數據集中,pandas 將一列推斷為 object 而不是 float64。 結果其中一行有 2,662 而不是 2.662,因為 pandas 沒有將其確定為 float64。

因此,我的建議是檢查數據以查看是否有任何行具有違規值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM