如何将包含'$'和','的字符串列转换为整数类型

问题描述 投票:0回答:1

我正在尝试在 pandas 中绘制数据框。数据框中有一个价格列,其中包含 $ 符号和浮点数。现在,如果我通过

car_sales\["Price"\]= car_sales\["Price"\].str.replace('\[$,.\]', '').astype(int)
将其转换为 int,我会收到如下错误:

`ValueError:以 10 为基数的 int() 的文字无效:'$4,000'

我该如何解决这个问题?

python pandas string integer valueerror
1个回答
0
投票

str.replace
的默认值现在是
regex=False
,显式传递
regex=True

car_sales["Price"]= car_sales["Price"].str.replace('[$,.]', '', regex=True).astype(int)
© www.soinside.com 2019 - 2024. All rights reserved.