I have a column in a dataframe named 'term'. its value is ('36 months', '60 months', '36 months', '36 months', '60 months') Now I want to convert that column to numeric like (36,60,36,36,60) How can I do that?
Asked
Active
Viewed 298 times
-3
-
1Lack of effort, because it takes fewer keystrokes for a google search than to log in and type a question here. – Reti43 Oct 18 '17 at 19:55
-
`(36,60,36,36,60)` is not a numeric dtype, it would still be `object` – juanpa.arrivillaga Oct 18 '17 at 19:57
-
1@WillemVanOnsem love that show! – Mangohero1 Oct 18 '17 at 19:57
-
`a.something = a.something.apply(lambda x: x.strip(' months')).astype(np.int64)`. I tested it, it works. Probably quite obscure though and intentionally so; please show some effort on your part. – roganjosh Oct 18 '17 at 19:57
-
Here's a helpful reference: https://stackoverflow.com/questions/4289331/python-extract-numbers-from-a-string – Markus L Oct 18 '17 at 20:39
1 Answers
1
This might help:
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

Mohamed Ali JAMAOUI
- 14,275
- 14
- 73
- 117