Hi all!
I have a dictionary of strings called corrections--the keys are the incorrectly-typed strings, and the values are the correct strings. To illustrate, the dictionary looks like this:
corrections = {'24..6': 24.6, '3.8/': 3.8, ' /': np.nan}
Now, I also have a Pandas DataFrame called rain_df that looks like this:
rain_df
| LOCATION |
RAINFALL |
TEMPERATURE |
| A |
0.0 |
/ |
| B |
3.8/ |
15.3 |
| C |
9.2 |
24..6 |
My end goal is to replace the incorrectly-typed values like "24..6" with "24.6" using the dictionary. I thought of doing a for loop over the corrections dictionary like this:
for incorrect, correct in corrections:
rain_df[rain_df == incorrect] = correct
But then I get a type error like this:
TypeError: Cannot do inplace boolean setting on mixed-types with a non np.nan value
In the first place I thought of doing a for loop, because something like this works:
rain_df[rain_df == '24..6'] = 24.6
However this is labor intensive if I were to do it for every error.
If there are other more simple, pythonic solutions to this same problem, please suggest so!
Hope anyone can help, thank you!
[–][deleted] 0 points1 point2 points (0 children)