'How to convert categorical data like Rs. 2,92,667 to integer and float type?
I'm trying to convert Rs. 2,92,667 to integer type but I'm confused like how to remove Rs. and commas from it .
cars = [int(x.split('Rs. ')[-1]) for x in cars['Ex-Showroom_Price']]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-14-b9318b3d5174> in <module>
----> 1 cars = [int(x.split('Rs. ')[-1]) for x in cars['Ex-Showroom_Price']]
<ipython-input-14-b9318b3d5174> in <listcomp>(.0)
----> 1 cars = [int(x.split('Rs. ')[-1]) for x in cars['Ex-Showroom_Price']]
ValueError: invalid literal for int() with base 10: '2,92,667'
Solution 1:[1]
Split the string and remove commas from the numeric part.
rs_string = 'Rs. 2,92,667'
rs_integer = int(rs_string.split()[-1].replace(',',''))
print(rs_integer)
Output:
292667
Note:
This will also work for a string that does not contain the 'Rs.' preamble but does assume that the resulting values can be converted to int. Just replace the call to int with float if required
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
