简体   繁体   English

选择Pandas中感兴趣的行之前和之后的行

[英]Selecting rows before and after rows of interest in Pandas

Let's say I have a time series dataframe with a categorical variable and a value: 假设我有一个带有分类变量和值的时间序列数据帧:

In [4]: df = pd.DataFrame(data={'category': np.random.choice(['A', 'B', 'C', 'D'], 11), 'value': np.random.rand(11)}, index=pd.date_range('2015-04-20','2015-04-30'))

In [5]: df
Out[5]:
           category     value
2015-04-20        D  0.220804
2015-04-21        A  0.992445
2015-04-22        A  0.743648
2015-04-23        B  0.337535
2015-04-24        B  0.747340
2015-04-25        B  0.839823
2015-04-26        D  0.292628
2015-04-27        D  0.906340
2015-04-28        B  0.244044
2015-04-29        A  0.070764
2015-04-30        D  0.132221

If I'm interested in rows with category A, filtering to isolate them is trivial. 如果我对类别A的行感兴趣,过滤以隔离它们是微不足道的。 But what if I'm interested in the n rows before category A's as well? 但是,如果我对A类之前的n行感兴趣呢? If n=2, I'd like to see something like: 如果n = 2,我想看到类似的东西:

In [5]: df[some boolean indexing]
Out[5]:
           category     value
2015-04-20        D  0.220804
2015-04-21        A  0.992445
2015-04-22        A  0.743648
2015-04-27        D  0.906340
2015-04-28        B  0.244044
2015-04-29        A  0.070764

Similarly, what if I'm interested in the n rows around category A's? 同样,如果我感兴趣的是围绕一个类别的第n行呢? Again if n=2, I'd like to see this: 再次,如果n = 2,我想看到这个:

In [5]: df[some other boolean indexing]
Out[5]:
           category     value
2015-04-20        D  0.220804
2015-04-21        A  0.992445
2015-04-22        A  0.743648
2015-04-23        B  0.337535
2015-04-24        B  0.747340
2015-04-27        D  0.906340
2015-04-28        B  0.244044
2015-04-29        A  0.070764
2015-04-30        D  0.132221

Thanks! 谢谢!

To answer your first question: 回答你的第一个问题:

df[pd.concat([df.category.shift(-i)=='A' for i in range(n)], axis=1).any(axis=1)]

You will hopefully be able to extend the same (perhaps a somewhat clumsy one) approach to cover more cases. 您希望能够扩展相同(可能有点笨拙)的方法来覆盖更多案例。

n rows around category A's: 类别A的n行:

In [223]: idx = df.index.get_indexer_for(df[df.category=='A'].index)

In [224]: n = 1

In [225]: df.iloc[np.unique(np.concatenate([np.arange(max(i-n,0), min(i+n+1, len(df)))
                                            for i in idx]))]
Out[225]:
           category     value
2015-04-20        D  0.220804
2015-04-21        A  0.992445
2015-04-22        A  0.743648
2015-04-23        B  0.337535
2015-04-28        B  0.244044
2015-04-29        A  0.070764
2015-04-30        D  0.132221

In [226]: n = 2

In [227]: df.iloc[np.unique(np.concatenate([np.arange(max(i-n,0), min(i+n+1, len(df)))
                                            for i in idx]))]
Out[227]:
           category     value
2015-04-20        D  0.220804
2015-04-21        A  0.992445
2015-04-22        A  0.743648
2015-04-23        B  0.337535
2015-04-24        B  0.747340
2015-04-27        D  0.906340
2015-04-28        B  0.244044
2015-04-29        A  0.070764
2015-04-30        D  0.132221

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM