简体   繁体   中英

How can I get a previous row from where the conditions are met in data frame in Pandas

I have very similar question to the one on this link , with only difference being to have multiple conditions met.

Let us assume that we have (sorted) dataframe, similar to the one in the example, with extra column:

    TIME    VALUE    EXTRA_FILTER
0   23:01   0        A
1   23:02   0        A
2   23:03   1        A
3   23:04   0        B
4   23:05   0        B
5   23:06   1        B
6   23:07   0        A
7   23:08   0        A
8   23:09   0        A
9   23:10   0        A
10  23:11   1        A
11  23:12   0        A      
12  23:13   0        A
13  23:14   0        A
14  23:15   0        A
15  23:16   1        A

And I would like to have extra column called PREV_TIME next to each of the rows, which will contain the previous value of TIME column where both conditions to have the column VALUE equals to 1, and column EXTRA_FILTER equals to A are met, something like this:

TIME    VALUE    EXTRA_FILTER    PREV_TIME
0   23:01   0        A            
1   23:02   0        A
2   23:03   1        A
3   23:04   0        B
4   23:05   0        B
5   23:06   1        B
6   23:07   0        A
7   23:08   0        A
8   23:09   0        A
9   23:10   0        A
10  23:11   1        A             23:03
11  23:12   0        A      
12  23:13   0        A
13  23:14   0        A
14  23:15   0        A
15  23:16   1        A             23:11

IIUC, use pandas.Series.shift :

df["PREV_TIME"] = df[df["VALUE"].eq(1) & df["EXTRA_FILTER"].eq("A")]["TIME"].shift()
df["PREV_TIME"].fillna("", inplace=True)
print(df)

Output:

     TIME  VALUE EXTRA_FILTER PREV_TIME
0   23:01      0            A          
1   23:02      0            A          
2   23:03      1            A          
3   23:04      0            B          
4   23:05      0            B          
5   23:06      1            B          
6   23:07      0            A          
7   23:08      0            A          
8   23:09      0            A          
9   23:10      0            A          
10  23:11      1            A     23:03
11  23:12      0            A          
12  23:13      0            A          
13  23:14      0            A          
14  23:15      0            A          
15  23:16      1            A     23:11

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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