python - 删除行(即用 NaN 填充)并在 Pandas Dataframe 中进行插值

标签 python pandas dataframe nan reindex

我正在处理一些时间序列数据,需要过滤掉一些垃圾。目标是保留时间戳并插入垃圾数据。

我尝试过过滤掉 int 并重新索引,但 python 似乎并没有以相同的方式对待日期时间索引。

所以,尝试过

ogIndex = df.index
df = df[df[col to filter] > some filter #]   # drops the index
df.reindex(ogIndex)

......没用

最佳答案

假设时间戳是数据帧的索引 - 不要使用 df = df[df[col to filter] > some filter] 删除它,只需反转过滤器(以选择您执行的行不想想要保留)并将过滤后的行设置为NaN:

import numpy as np

df[df[col to filter] < some filter] = np.nan

这会保留索引并使该行适用于插值。之后,您可以使用插值方法,例如像这样的东西:

df.interpolate(method='linear', limit_direction='forward', axis=0)

关于python - 删除行(即用 NaN 填充)并在 Pandas Dataframe 中进行插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60170438/

相关文章:

Python-Camelot 提取空表

python - 使用 df.apply() 将带参数的函数应用于每一行

python - 将不规则时间戳的 Dataframe 对象,将价格和数量信息转换为等距的、数量加权平均价格

python - 在Python中循环日期

python - 将 pandas hub_table 与 Interval 列一起使用会导致 TypeError

Python 软件开发(CSV 到 Pandas 到 SQL 或 CSV 到 SQL 到 Pandas)

python - 从 python 中的字母数字列中删除字符串

Python - 在函数内部定义常量

python - 占位符的尺寸如何用于 tensorflow ?

python - 在 Ubuntu 中使用 python 而不是 python3 运行