我正在处理一些时间序列数据,需要过滤掉一些垃圾。目标是保留时间戳并插入垃圾数据。
我尝试过过滤掉 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/