首页 > 解决方案 > 数据包含特定格式的错误读数

问题描述

我有一个 pandas df,其中我的一列有错误的值。我想清理这些值

错误值为负数并以 < 结尾,例如“-2.44<”。如何在不影响其他列的情况下解决此问题?我的索引是日期时间

我试图将列转换为数字数据。

df.values = pd.to_numeric(df.values, errors='coerce')

没有错误消息。但是,我想用删除'<'来替换它们。

标签: pythonpandasdata-sciencedata-processing

解决方案


用于从右侧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')

推荐阅读