[英]Where col is equal to x return next ordered value in list - Pandas
[英]Pandas: How to return the rows where col value is greater than 'x' in rolling window
我有一个很大的 df,我正在尝试查找特定列中的值高于给定数字但在 3 行的窗口内的所有行,并仅返回超过给定数字的最高值的行。
A B C D E
1 5 9 10 15
2 4 7 12 16
3 3 5 10 18
4 2 3 15 17
5 1 1 10 14
6 5 9 17 13
7 4 7 10 14
8 3 5 19 19
9 2 3 10 18
10 4 7 5 14
11 3 5 6 19
12 2 3 7 18
如果我想用上面的 D 列示例执行此操作,其中值必须大于 11,输出将是。
A B C D E
2 4 7 12 16
6 5 9 17 13
8 3 5 19 19.
解决这个问题的最佳方法是什么?
我试过: df.rolling(3,win_type=None, on='D')
但找不到包含大于条件的方法。
任何帮助表示赞赏。 谢谢!
编辑:试试这个:
threshold = 11
window = 3
df['r'] = np.floor(df.index / window)
print(df.groupby('r').apply(lambda x : (x.loc[x['D'] == x['D'].max() ,:]) if x['D'].max() > threshold else None))
您可以在使用后删除列 'r'。
输出:
A B C D E r
r
0.0 1 2 4 7 12 16 0.0
1.0 5 6 5 9 17 13 1.0
2.0 7 8 3 5 19 19 2.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.