python - 从列中过滤掉非数字值
问题描述
我在 pandas DataFrame 中有以下列:
col1
1.2
1.4
3.1
aa
bb
NaN
我需要计算列中的最小值,col1
同时忽略所有空值和非数字值。
如果我这样做df[col1].min()
,它只会忽略空值,但我仍然会收到此错误:
TypeError: '<=' not supported between instances of 'float' and 'str'
解决方案
pd.to_numeric(df.col1,errors='coerce').min()
#1.2
#or df.col1.apply(lambda x: pd.to_numeric(x,errors='coerce')).min() <- slow
推荐阅读
- go - 拥有供应商文件夹有什么好处?
- swift - 使用 didSelectItemAt Indexpath 更改购物清单中的值
- python-3.x - 如何根据画布对象的大小更改滚动条的滑块长度?
- python - Python 使用基于前 3 行的滚动窗口向 DataFame 添加列
- java - 如何以编程方式设置数据库连接参数
- asp.net-core - ASP.NET Core 3.1 Razor - 是否可以使用 Html.Raw 将字符串转换为超链接
- ios - NavigationLink 项目区域内的 SwiftUI 按钮无效
- vue.js - 防止在使用 Axios 拦截器时将错误记录到控制台
- maven - 目标 org.apache.maven.plugins:maven-deploy-plugin:3.0.0-M1:deploy-file 的参数“url”丢失或无效
- sql - 在 case 表达式中向 else 添加逻辑以处理空值