2017-10-18 42 views
-4

我在名爲'term'的數據框中有一列。它的值是('36個月,'60個月','36個月','36個月','60個月') 現在我想將該列轉換爲數字,如(36,60,36,36,60 ) 我該怎麼做?如何將字符串轉換爲python中的數字

+1

缺少,因爲它需要較少的按鍵爲谷歌搜索,而不是登錄並在這裏輸入一個問題。 – Reti43

+0

'(36,60,36,36,60)'不是一個數字dtype,它仍然是'object' –

+1

@WillemVanOnsem愛這個節目! – Mangohero1

回答

1

這可能幫助:努力

In [12]: df 
Out[12]: 
     term 
0 36 months 
1 60 months 
2 36 months 
3 36 months 
4 60 months 

In [14]: df.term.str.replace(' months','').astype(int) 
Out[14]: 
0 36 
1 60 
2 36 
3 36 
4 60 
Name: term, dtype: int64 
相關問題