Reputation: 330
So I've got a column in a dataframe that is filled with float values and occasional string values. I've tried following some answers on stack but it just doesnt work.
print(data['Snowfall'][48609]) #prints #VALUE!
print(type(data['Snowfall'][48609])) #prints <class 'str'>
data['Snowfall'].str.contains("#VALUE!").replace(float(0.0),inplace=True)
print(type(data['Snowfall'][48609])) # prints <class 'str'>
what am i doing wrong
Upvotes: 2
Views: 83
Reputation: 18647
Use pandas.to_numeric
passing 'coerce' to the errors
argument.
Then Series.fillna
to change coerced values to 0
df['Snowfall'] = pd.to_numeric(df['Snowfall'], errors='coerce').fillna(0)
Upvotes: 7