问题描述
我试图将所有带有$"数量的列从对象转换为浮点类型.
i trying to convert all columns with '$' amount from object to float type.
使用下面的代码,我无法删除 $ 符号.
with below code i couldnt remove the $ sign.
输入:
df[:] = df[df.columns.map(lambda x: x.lstrip('$'))]
推荐答案
你可以使用extract
df=pd.dataframe({'a':['$10.00','$10.00','$10.00']})
df.apply(lambda x : x.str.extract('(d )',expand=false).astype(float))
out[333]:
a
0 10.0
1 10.0
2 10.0
更新
df.iloc[:,9:32]=df.iloc[:,9:32].apply(lambda x : x.str.extract('(d )',expand=false).astype(float))
丿色胚