简体   繁体   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')

I have tried to convert an object dtype column to float64 using .astype('float64') It ran without raising any error, but when I check the dtype using.dtype or.dtypes it is showing that converted column again as object.我尝试使用.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

Why is it not converting and why isn't it giving any error?为什么它不转换,为什么不给出任何错误?

also, real_estate['Age at time of purchase'].dtype this is giving me something that I haven't expected.此外, real_estate['Age at time of purchase'].dtype这给了我一些我没想到的东西。 dtype('O') What does dtype('O') mean? dtype('O') dtype('O')是什么意思?

你可以这样试试

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

In one of my datasets, pandas was inferring a column as object instead of float64.在我的一个数据集中,pandas 将一列推断为 object 而不是 float64。 Turned out one of the rows had 2,662 instead of 2.662 because of which pandas did not determine it as float64.结果其中一行有 2,662 而不是 2.662,因为 pandas 没有将其确定为 float64。

So, my recommendation will be to inspect the data to see if any rows have offending value.因此,我的建议是检查数据以查看是否有任何行具有违规值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM