使用
Series.replace
转换为
float
由
Series.astype
:
df2.PRICE = df2.PRICE.replace('[\$,]','', regex=True).astype(float)
print (df2)
PRICE
0 179000.0
1 110000.0
2 275000.0
3 140000.0
4 180000.0
564611 85500.0
564612 80800.0
564613 74500.0
564614 75900.0
564615 66700.0
如果有
integers
:
df2.PRICE = df2.PRICE.replace('[\$,]','', regex=True).astype(float).astype(int)
print (df2)
PRICE
0 179000
1 110000
2 275000
3 140000
4 180000
564611 85500
564612 80800
564613 74500
564614 75900
564615 66700
如果转换为浮动失败,则使用
to_numeric
具有
errors='coerce'
对于无法转换为数字的缺少值:
df2.PRICE = pd.to_numeric(df2.PRICE.replace('[\$,]','', regex=True), errors='coerce')