繁体   English   中英

Dataframe 如果在 dataframe 列上满足条件,那么在 dataframe 中获取这一行和接下来的 3 行?

[英]Dataframe if a condition is met on a dataframe column then get this row and the next 3 rows in a dataframe?

我想编写一个代码,以便如果在 dataframe 列上满足条件,那么我可以在 dataframe 中获得这一行和接下来的 3 行?

每次我的资金费率大于 0.0363 时,我都想要该行和匹配行的下 3 行。

在此处输入图像描述

您可以使用内置模块functools中的reduce来动态执行此操作:

import functools

N = 3
cond = df['Funding Rate'].str.strip('%').astype(float).gt(0.0363)
df[functools.reduce(lambda x, y: x | y, [cond.shift(i) for i in range(N + 1)])]

暂无
暂无

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

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