我正在尝试在 pandas 中绘制数据框。数据框中有一个价格列,其中包含 $ 符号和浮点数。现在,如果我通过
car_sales\["Price"\]= car_sales\["Price"\].str.replace('\[$,.\]', '').astype(int)
将其转换为 int,我会收到如下错误:
`ValueError:以 10 为基数的 int() 的文字无效:'$4,000'
我该如何解决这个问题?
str.replace
的默认值现在是regex=False
,显式传递regex=True
。
car_sales["Price"]= car_sales["Price"].str.replace('[$,.]', '', regex=True).astype(int)