问题描述
我试图将所有带有$"数量的列从对象转换为浮点类型.
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))