本文介紹了pandas - 如何將所有列從對象轉(zhuǎn)換為浮點(diǎn)類型的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我試圖將所有帶有$"數(shù)量的列從對象轉(zhuǎn)換為浮點(diǎn)類型.
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))
這篇關(guān)于pandas - 如何將所有列從對象轉(zhuǎn)換為浮點(diǎn)類型的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!