Issue
I have a column in my dataframe which looks like this:
col
12
77
15
"UNKNOWN"
12
dtype: object
I want to replace "UNKNOWN"
with NA
and turn object into float type. But when I do this:
df["col"].replace({"UNKNOWN": np.nan}, inplace=True)
This error appears: TypeError: Cannot compare types 'ndarray(dtype=float64)' and 'str'
.
How to solve this?
Solution
Instead of replacing it might be better if you use pd.to_numeric
with errors='coerce'
that will automatically turn string like values into np.nan
and also convert your column to numeric
type:
df = pd.DataFrame({'col':[12,77,15,'"UNKNOWN"',12]})
df['col'] = pd.to_numeric(df['col'], errors='coerce')
print(df)
col
0 12.0
1 77.0
2 15.0
3 NaN
4 12.0
Answered By - sophocles
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.