数据列值未更改为浮动 [英] Data column values are not changing to float

查看:73
本文介绍了数据列值未更改为浮动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,

df,
    Name    Stage   Description
0   sri      1      sri is one of the good singer in this two
1   nan      2      thanks for reading
2   ram      1      ram is two of the good cricket player
3   ganesh   1      one driver
4   nan      2      good buddies

tried df["Stage"]=pd.to_numeric(df["Stage"],downcast="float")

但值仍然相同

推荐答案

您可以使用df.Stage.astype(float):

In [6]: df.Stage.astype(float)
Out[6]: 
0    1.0
1    2.0
2    1.0
3    1.0
4    2.0
Name: Stage, dtype: float64

In [7]: df.Stage.astype(float)


使用pd.to_numeric更好,因为它可以处理到占用较少内存的float类型的转换.


Using pd.to_numeric is better as it handles the conversion to a float type that takes less memory.

示例

In [23]: df.Stage 
Out[23]: 
0    1
1    2
2    1
3    1
4    2
Name: Stage, dtype: int64

In [24]: import sys 

In [25]: sys.getsizeof(df.Stage)
Out[25]: 272

In [26]: sys.getsizeof(df.Stage.astype(float))
Out[26]: 272

In [27]: sys.getsizeof(pd.to_numeric(df.Stage, downcast='float'))
Out[27]: 252

如果df.Stage中有错误数据,则将值强制为NaN pd.to_numeric(df.Stage, errors='coerce', downcast='float')

In case there are bad data in df.Stage, coerce the value to NaN pd.to_numeric(df.Stage, errors='coerce', downcast='float')

这篇关于数据列值未更改为浮动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆