-1

I am willing to filter the ids which have SMS and Phone in the type column and whenever login_method is equal to resend

df

id    type   login_method
 1     SMS         resend
 1     SMS       complete
 2   phone         resend
 2     SMS         resend
 2     SMS          start
 3   phone         resend
 3   phone          start
 3   phone       complete
 3     SMS           nice

expected result

df
    id    type   login_method
 1     SMS         resend
 1     SMS       complete
 3   phone         resend
 3   phone          start
 3   phone       complete
 3     SMS           nice

In this case only id 2 have phone and SMS in the login method equal to resend

Lucas Dresl
  • 1,150
  • 1
  • 10
  • 19

1 Answers1

1

Use:

v = ['SMS','phone']
#first filter only valuse by list
df = df[df['type'].isin(v)]

#get id where are all values per groups with resend
m1 = df['login_method'] == 'resend'
s = df[m1].drop_duplicates(['id','type']).groupby('id')['type'].nunique() != len(v)

#filtering by ids
df1 = df[df['id'].isin(s.index[s])]
print (df1)
   id   type login_method
0   1    SMS       resend
1   1    SMS     complete
4   3  phone       resend
5   3  phone        start
6   3  phone     complete
7   3    SMS         nice
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252