简体   繁体   English

如何基于当前行的条件获取Pandas GroupedBy Dataframe的前一行?

[英]How to get previous rows of a pandas GroupedBy Dataframe based on a condition on the current row?

I have a dataframe like this: 我有这样的数据帧:

StringCol Timestamp GroupID Flag
   xyz    20170101   123     yes
   abc    20170101   123     yes
   def    20170101   123     yes
   ghi    20170101   123     no
   abc    20170101   124     yes
   jkl    20170101   124     yes
   pqr    20170101   124     no
   klm    20170101   124     yes

I want to group this by the GroupID, and for each group, I want the rows that have flag as "no" and X number of previous rows before it (the dataframe is sorted by GroupID and Timestamp already). 我想通过GroupID对此进行分组,对于每个组,我希望标记为“no”的行和之前的前一行的X个数(数据帧已按GroupID和Timestamp排序)。

So, if X = 2, I want the result to be something like: 所以,如果X = 2,我希望结果如下:

StringCol Timestamp GroupID Flag
   abc    20170101   123     yes
   def    20170101   123     yes
   ghi    20170101   123     no
   abc    20170101   124     yes
   jkl    20170101   124     yes
   pqr    20170101   124     no

How do I achieve this? 我该如何实现这一目标? Thanks. 谢谢。

This gets the previous X items for the last flag per group. 这将获得每组最后一个标志的前X项。

def prevK(x):
    i = x.reset_index(drop=True).Flag.eq('no').iloc[::-1].idxmax()
    return x.iloc[i - 2:i + 1, :]

df.groupby('GroupID', group_keys=False).apply(prevK)

  StringCol  Timestamp  GroupID Flag
1       abc   20170101      123  yes
2       def   20170101      123  yes
3       ghi   20170101      123   no
4       abc   20170101      124  yes
5       jkl   20170101      124  yes
6       pqr   20170101      124   no

If you only need last no in the group try drop_duplicates 如果你只需要组中的最后一个,请尝试drop_duplicates

df1=df.copy()
df=df[df['Flag'].eq('no')].drop_duplicates(['GroupID'],keep='last')

idx=df.index+1
idy=df.index-2
import itertools
df1.loc[list(itertools.chain(*[list(range(y,x)) for x , y in  zip(idx,idy)]))]
Out[512]: 
  StringCol  Timestamp  GroupID Flag
1       abc   20170101      123  yes
2       def   20170101      123  yes
3       ghi   20170101      123   no
4       abc   20170101      124  yes
5       jkl   20170101      124  yes
6       pqr   20170101      124   no

暂无
暂无

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

相关问题 如何在 Pandas 的 DataFrame 中获取带有条件的前一行 - How to get previous row with condition in a DataFrame of Pandas 如何根据 pandas DataFrame 中的条件从当前行值中减去前一行值? - how to subtract previous row value from current row value based on condition in pandas DataFrame? 基于前一行和当前行的多个 IF 条件 - Pandas - Multiple IF condition based on previous and the current row - Pandas 根据条件删除 Pandas Dataframe 中的前一行 - Delete previous rows in Pandas Dataframe based on condition Pandas dataframe 如果其他条件基于先前行不工作 - Pandas dataframe if else condition based on previous rows not working Pandas Dataframe根据条件通过上一个更新行值 - Pandas Dataframe update the row values by previous one based on condition 如何将数据帧的每一行与以下两行进行比较,并基于这三行和一种算法来修改当前行? (熊猫) - How to compare each row of a dataframe to the following 2 rows, and modify the current row based on these 3 rows and an algorithm? (Pandas) Pandas dataframe 如果其他条件基于先前行不工作 - Pandas dataframe if else condition based on previous rows not working 如何根据多个条件根据前一行填充 pandas dataframe 列的行? - How to populate rows of pandas dataframe column based with previous row based on a multiple conditions? 如何根据前一行合并数据框中的行? - How to merge rows in a Dataframe based on a previous row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM