简体   繁体   中英

Select all rows from where a condition is true in pandas

I have a dataframe

 Id  Seqno. Event
 1     2    A 
 1     3    B 
 1     5    A 
 1     6    A 
 1     7    D
 2     0    E
 2     1    A 
 2     2    B 
 2     4    A 
 2     6    B

I want to get all the events happened since the count of recent occurrence of Pattern A = 2 for each ID. Seqno. is a sequence number for each ID. The output will be

 Id  Seqno. Event 
 1     5    A 
 1     6    A 
 1     7    D
 2     1    A 
 2     2    B 
 2     4    A 
 2     6    B

so far i tried,

  y=x.groupby('Id').apply( lambda 
  x:x.eventtype.eq('A').cumsum().tail(2)).reset_index()
  p=y.groupby('Id').apply(lambda x:       
  x.iloc[0]).reset_index(drop=True)
  q= x.reset_index()
  s= pd.merge(q,p,on='Id')
  dd= s[s['index']>=s['level_1']]

I was wondering if there is a good way of doing it.

Use groupby with cumsum , subtract it from the count of A's per group, and filter:

g = df['Event'].eq('A').groupby(df['Id'])
df[(g.transform('sum') - g.cumsum()).le(1)]

   Id  Seqno. Event
2   1       5     A
3   1       6     A
4   1       7     D
6   2       1     A
7   2       2     B
8   2       4     A
9   2       6     B

Thanks to cold ,ALollz and Vaishali, via the explanation (from the comment) using groupby with cumcount get the count , then we using reindex and ffill

s=df.loc[df.Event=='A'].groupby('Id').cumcount(ascending=False).add(1).reindex(df.index)
s.groupby(df['Id']).ffill()
Out[57]: 
0    3.0
1    3.0
2    2.0
3    1.0
4    1.0
5    NaN
6    2.0
7    2.0
8    1.0
9    1.0
dtype: float64
yourdf=df[s.groupby(df['Id']).ffill()<=2]
yourdf
Out[58]: 
   Id  Seqno. Event
2   1       5     A
3   1       6     A
4   1       7     D
6   2       1     A
7   2       2     B
8   2       4     A
9   2       6     B

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