dataframe列中的值不会从string更改为float

Bor*_*koP 2 python pandas

所以我在数据框中有一个列,它填充了浮点值和偶尔的字符串值.我试过在堆栈上关注一些答案,但它不起作用.

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'>
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么

Chr*_*s A 7

使用pandas.to_numeric传递'coerce'到errors参数.然后Series.fillna将强制值更改为0

df['Snowfall'] = pd.to_numeric(df['Snowfall'], errors='coerce').fillna(0)
Run Code Online (Sandbox Code Playgroud)