简体   繁体   English

基于另一列的条件填充

[英]Conditional ffill based on another column

I'm trying to conditionally ffill a value until a second column encounters a value and then reset the first column value.我试图有条件地填充一个值,直到第二列遇到一个值,然后重置第一列值。 Effectively the first column is an 'on' switch until the 'off' switch (second column) encounters a value.实际上,第一列是一个“开”开关,直到“关”开关(第二列)遇到一个值。 I've yet to have a working example using ffill and where.我还没有一个使用 ffill 和 where 的工作示例。

Example input:示例输入:

Index   Start   End
0       0       0
1       0       0
2       1       0
3       0       0
4       0       0
5       0       0
6       0       1
7       0       0
8       1       0
9       0       0
10      0       0
11      0       0
12      0       1
13      0       1
14      0       0

Desired output:所需的 output:

Index   Start   End
0       0       0
1       0       0
2       1       0
3       1       0
4       1       0
5       1       0
6       1       1
7       0       0
8       1       0
9       1       0
10      1       0
11      1       0
12      1       1
13      0       1
14      0       0

EDIT:编辑:

There are issues when dealing with values set based on another column.处理基于另一列设置的值时会出现问题。 The logic is as follows: Start should be zero until R column is below 25, then positive until R column is above 80 and the cycle should repeat.逻辑如下:开始应该为零,直到 R 列低于 25,然后为正,直到 R 列高于 80,并且循环应该重复。 Yet on row 13 Start is inexplicably set 1 despite not matching criteria.然而,尽管标准不匹配,但在第 13 行 Start 莫名其妙地设置为 1。

df = pd.DataFrame(np.random.randint(0, 100, size=100), columns=['R'])
df['Start'] = np.where((df.R < 25), 1, 0)
df['End'] = np.where((df.R > 80), 1, 0)
df.loc[df['End'].shift().eq(0), 'Start'] = df['Start'].replace(0, np.nan).ffill().fillna(0).astype(int)
        R   Start  End
0       58  0       0
1       98  0       1
2       91  0       1
3       69  0       0
4       55  0       0
5       57  0       0
6       64  0       0
7       75  0       1
8       78  0       1
9       90  0       1
10      24  1       0
11      89  1       1
12      36  0       0
13      70  **1**   0

Try:尝试:

df.loc[df['End'].shift().eq(0), 'Start'] = df['Start'].replace(0, np.nan).ffill().fillna(0).astype(int)

[out] [出去]

    Start  End
0       0    0
1       0    0
2       1    0
3       1    0
4       1    0
5       1    0
6       1    1
7       0    0
8       1    0
9       1    0
10      1    0
11      1    0
12      1    1
13      0    1
14      0    0

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

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