Python 将列从对象转换为浮点时出现值错误
我正在尝试将一列“value$”从数据类型“Object”转换为“float”,因为该列将涉及数值计算 我最初使用以下内容替换本栏中的“$”: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
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')