Pandas检查之前的n行是否满足条件

huangapple go评论87阅读模式
英文:

Pandas check if any of previous n rows met criteria

问题

以下是您要翻译的部分:

# get latest 3 records
df['blue_condition'] = df.tail(3)

# assign using lambda
df['blue_condition'].assign(blue_condition=lambda x: (x.tail(3).query(top < top.tail(3))))

请注意,这段代码似乎包含了HTML转义字符,如果需要运行,请将其替换为正常的Python代码。

英文:

Say i have this data set.

|    | color  | down | top |
| -- | ------ | ---- | --- |
| 0  |        | 1    | 5   |
| 1  |        | 2    | 5   |
| 2  | blue   | 7    | 11  |
| 3  |        | 5    | 8   |
| 4  |        | 9    | 10  |
| 5  |        | 9    | 10  |
| 6  | orange | 5    | 9   |
| 7  |        | 4    | 7   |
| 8  |        | 5    | 10  |
| 9  |        | 5    | 6   |
| 10 |        | 3    | 7   |

I want to flag rows where any of the 3 previous rows are either blue or orange AND where current top is between down and top of that row.

The outcome of this operation would be a dataset:

|    | color  | down | top | blue_condition | orange_condition |
| -- | ------ | ---- | --- | -------------- | ---------------- |
| 0  |        | 1    | 5   |                |                  |
| 1  |        | 2    | 5   |                |                  |
| 2  | blue   | 7    | 11  |                |                  |
| 3  |        | 5    | 8   | 1              |                  |
| 4  |        | 9    | 10  | 1              |                  |
| 5  |        | 9    | 10  | 1              |                  |
| 6  | orange | 5    | 9   |                |                  |
| 7  |        | 4    | 7   |                | 1                |
| 8  |        | 5    | 10  |                |                  |
| 9  |        | 5    | 6   |                | 1                |
| 10 |        | 3    | 7   |                |                  |

I have been fiddling around with combinations between .tail(), .filter() and .assign(). But I am a bit stuck to be honest.

df = pd.DataFrame({&quot;color&quot;: [None, None, &#39;blue&#39;, None, None, None, &#39;orange&#39;, None, None, None, None],
                   &#39;down&#39;: [1, 2, 7, 5, 9, 9, 5, 4, 5, 5, 3],
                   &#39;top&#39;: [5, 5, 11, 8, 10, 10, 9, 7, 10, 6, 7]})

# get latest 3 records
df[&#39;blue_condition&#39;] = df.tail(3)

# assign using lambda
df[&#39;blue_condition&#39;].assign(blue_condition=lambda x: (x.tail(3).query(top &lt; top.tail(3))))

I have looked into other questions but they don't take the current row as a reference, from what I can tell.
https://stackoverflow.com/questions/74734980/pandas-return-true-if-condition-true-in-any-of-previous-n-rows

https://stackoverflow.com/questions/56573008/compare-the-previous-n-rows-to-the-current-row-in-a-pandas-column

答案1

得分: 2

只返回翻译好的代码部分:

N = 3
df2 = (df.pivot(columns='color', values=['top', 'down'])
         .drop(columns=np.nan, level=1)
         .ffill(limit=N-1).shift()
      )

out = df.join((df2['down'].le(df['top'], axis=0)
             & df2['top'].ge(df['top'], axis=0)).astype(int))
英文:

You can use a pivot, then ffill+shift the data and use this to compare to the top value:

N = 3
df2 = (df.pivot(columns=&#39;color&#39;, values=[&#39;top&#39;, &#39;down&#39;])
         .drop(columns=np.nan, level=1)
         .ffill(limit=N-1).shift()
      )

out = df.join((df2[&#39;down&#39;].le(df[&#39;top&#39;], axis=0)
             &amp; df2[&#39;top&#39;].ge(df[&#39;top&#39;], axis=0)).astype(int))

Output:

     color  down  top  blue  orange
0     None     1    5     0       0
1     None     2    5     0       0
2     blue     7   11     0       0
3     None     5    8     1       0
4     None     9   10     1       0
5     None     9   10     1       0
6   orange     5    9     0       0
7     None     4    7     0       1
8     None     5   10     0       0
9     None     5    6     0       1
10    None     3    7     0       0

huangapple
  • 本文由 发表于 2023年3月7日 01:22:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653925.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定