Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将列从对象转换为浮点时出现值错误_Python_Pandas - Fatal编程技术网

Python 将列从对象转换为浮点时出现值错误

Python 将列从对象转换为浮点时出现值错误,python,pandas,Python,Pandas,我正在尝试将一列“value$”从数据类型“Object”转换为“float”,因为该列将涉及数值计算 我最初使用以下内容替换本栏中的“$”: df['Value $'] = df['Value $'].replace({'\$': ''},regex=True) 然后使用以下命令将其转换为数字: df['Value $'] = df['Value $'].astype(dtype=np.float64) 可能是因为您还需要替换逗号: df['Value $'] = df['Value

我正在尝试将一列“value$”从数据类型“Object”转换为“float”,因为该列将涉及数值计算

我最初使用以下内容替换本栏中的“$”:

 df['Value $'] = df['Value $'].replace({'\$': ''},regex=True)
然后使用以下命令将其转换为数字:

 df['Value $'] = df['Value $'].astype(dtype=np.float64)

可能是因为您还需要替换逗号:

df['Value $'] = df['Value $'].replace({'\$|,': ''}, regex=True)
df['Value $'] = df['Value $'].astype(dtype=np.float64)

可能是因为您还需要替换逗号:

df['Value $'] = df['Value $'].replace({'\$|,': ''}, regex=True)
df['Value $'] = df['Value $'].astype(dtype=np.float64)

你可以用这个来代替

df['Value $'] = df['Value $'].fillna(0.0).str.replace('[$,]', '').astype('float')

你可以用这个来代替

df['Value $'] = df['Value $'].fillna(0.0).str.replace('[$,]', '').astype('float')