如何将对象转换为浮在数据框中

问题描述

每列中都有一个数据框,我有数字,但类型是对象!如何将对象转换为浮点型?

我尝试过这个: pd.to_numeric(df['Close_x'],errors='coerce')

但是错误提示:

TypeError: arg must be a list,tuple,1-d array,or Series

我尝试了concatenated['Close_x'].astype(float)

错误提示:

ValueError: could not convert string to float: '1,001.96'

解决方法

这些数字以,作为分隔符,因此,首先,您需要将其替换为空字符串,然后将其转换为浮点数。

df = pd.DataFrame(['1,001.96','1001.98'],columns=['value'])
pd.to_numeric(df['value'].replace(',','',regex=True))

df.apply(lambda x: x.str.replace(','')).astype(float)

注意:对于大数据帧,df.applypd.to_numeric

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...