所以我在数据框中有一个列,它填充了浮点值和偶尔的字符串值.我试过在堆栈上关注一些答案,但它不起作用.
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)
我究竟做错了什么
使用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)
| 归档时间: |
|
| 查看次数: |
69 次 |
| 最近记录: |