Data column values are not changing to float

牧云@^-^@ 提交于 2019-12-10 23:13:58

问题


I have a dataframe,

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")

but still the values are same


回答1:


You can use 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)

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

Example

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

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




回答2:


I think you need astype:

df["Stage"]=df["Stage"].astype(float)

If first solution failed because some non numeric data, use to_numeric with parameter errors='coerce' for replace bad data to NaNs, so output are floats:

df["Stage"]=pd.to_numeric(df["Stage"],errors="coerce")


来源:https://stackoverflow.com/questions/46709333/data-column-values-are-not-changing-to-float

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!