python - 数据包含特定格式的错误读数
问题描述
我有一个 pandas df,其中我的一列有错误的值。我想清理这些值
错误值为负数并以 < 结尾,例如“-2.44<”。如何在不影响其他列的情况下解决此问题?我的索引是日期时间
我试图将列转换为数字数据。
df.values = pd.to_numeric(df.values, errors='coerce')
没有错误消息。但是,我想用删除'<'来替换它们。
解决方案
用于从右侧Series.str.rstrip
移除:<
df.values = pd.to_numeric(df.values.str.rstrip('<'), errors='coerce')
或者更一般地使用Series.str.strip
- 可能添加更多值:
df.values = pd.to_numeric(df.values.str.strip('<>'), errors='coerce')
推荐阅读
- python-3.x - 我怎样才能不断改变时间
- javascript - 如何在 Javascript 中将 Date 对象设置为 null?
- groovy - Groovy 合并 YAML 文件而不重复
- flutter - 如何在 Dart 的 Sound Null Safety 中增加/减少可为空的表达式:`
!++`? - floating-point - 64位浮点计算是否精确
- c++ - Visual Studio Code C/C++“强制包含”有效吗?
- nginx - 多根nginx
- python - 通过 Docker SDK for Python 构建 docker 容器时指定平台
- html - 在 HTML 和 CSS 中制作下拉菜单
- r - ggplot - y 轴的顺序与数据集的顺序不同