简体   繁体   English

如何从 Pandas 数据框中满足条件的位置获取前一行

[英]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:我想在每一行旁边都有一个名为 PREV_TIME 的额外列,它将包含 TIME 列的前一个值,其中满足列 VALUE 等于 1 和列 EXTRA_FILTER 等于 A 的两个条件,类似这样:

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 : IIUC,使用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

暂无
暂无

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

相关问题 如何从Pandas中的数据框中获取满足条件的前一行 - How can I get a previous row from where the condition is met in data frame in Pandas 如何从(切片)pandas 数据帧中的前一行获取值? - How to get a value from of the previous row in a (sliced) pandas data frame? 如何对熊猫数据框计算进行矢量化,如果不满足条件,则输入前一行的数据? - How to vectorize a pandas dataframe calculation where if a conditional is not met the data from the previous row is entered? 如何根据该行中的条件为另一个数据框中的每一行选择一个数据框? - How can I get a selection of one data frame for each row in another data frame based on conditions in that row? 如何根据条件获取前dataframe行的数据? - How to get data from previous dataframe row based on conditions? 如何遍历具有固定列的熊猫数据框的每一行并根据python中的条件执行操作? - How can I iterate over each row of a pandas data frame with a fixed column and perform operation on the basis of conditions in python? 如何获取 pandas 列中的前一行值 - how can I get a previous row value in pandas column 如何使用基于上一行和下一行的条件在 Pandas Dataframe 上创建新列? - How can I create a new column on a Pandas Dataframe with conditions based on previous and next row? 如何将熊猫数据框的第 n 行提取为熊猫数据框? - How can I extract the nth row of a pandas data frame as a pandas data frame? 如何使用 pandas 根据前一行值填充数据框? - How to populate data frame based on previous row values with pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM