BorkoP
BorkoP

Reputation: 330

Value in dataframe column wont change from string to float

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

Answers (1)

Chris Adams
Chris Adams

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

Related Questions