以10为底的int()无效文字:'-1,100.00'。 abs()函数

时间:2018-11-04 22:29:40

标签: pandas

p['WinRx Per Person']

0      -1,100.00
1      -1,100.00
2      -1,850.00
3           -200
4           -925
5           -925
6      -1,100.00
7      -1,850.00
8           -925
9           -925
10     -1,850.00
11     -1,850.00
12     -1,100.00
13     -1,850.00
14     -1,100.00

type('WinRx Per Person')

str

p['WinRx Per Person'].astype(int)

invalid literal for int() with base 10: '-1,100.00'

当我尝试将字符串类型转换为int以便使用abs()函数时,会抛出错误

1 个答案:

答案 0 :(得分:1)

to_numeric之后使用replace

pd.to_numeric(df['WinRx Per Person'].str.replace(',',''))
Out[248]: 
0    -1100.0
1    -1100.0
2    -1850.0
3     -200.0
4     -925.0
5     -925.0
6    -1100.0
7    -1850.0
8     -925.0
9     -925.0
10   -1850.0
11   -1850.0
12   -1100.0
13   -1850.0
14   -1100.0
Name: WinRx Per Person, dtype: float64