如何将字符串数组($0.00格式)转换为小数

2024-05-15 00:02:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将代码中当前为字符串$格式的列转换为数字

我的代码当前为:

s=top_performing_schools["Per Student Budget"]

top_performing_schools["Per Student Budget"]=pd.to_numeric(s)

我得到一个错误:

ValueError: Unable to parse string "$582.00" at position 0

Tags: to字符串代码top格式错误数字student
1条回答
网友
1楼 · 发布于 2024-05-15 00:02:09

您可以删除$符号,然后应用数字转换,例如:

import pandas as pd


df = pd.DataFrame(
    {"Per Student Budget": ["$582.00", "$100", "$12000"]}
)

df["Per Student Budget"] = pd.to_numeric(df["Per Student Budget"].str.replace('$', ''))
print(df)

   Per Student Budget
0               582.0
1               100.0
2             12000.0

相关问题 更多 >

    热门问题