I have a DataFrame that is all numeric and contains some error values such as :10..9,10...9 etc. I want to keep only one decimal place. What do I do with that?
Asked
Active
Viewed 59 times
1 Answers
0
Example data:
values = ['10.9', '10..9', '10...9']
Using re
:
list(map(lambda x: float(re.sub('\.+', '.', x)), values))
[10.9, 10.9, 10.9]
Using pandas
:
s = pd.Series(values)
s.str.replace('\.+', '.').astype(float)
0 10.9
1 10.9
2 10.9
dtype: float64

Cainã Max Couto-Silva
- 4,839
- 1
- 11
- 35
-
-
Yes, because there're no numbers with two points in Python, like the user provided. I assumed the OP actually have strings and would like to to strip off additional points (in order to make them numeric). – Cainã Max Couto-Silva Nov 16 '20 at 06:53
-
That's the reason, I think, the user maked `re` in the tags. But I might be wrong. – Cainã Max Couto-Silva Nov 16 '20 at 06:54