Is there a way to filter a dataframe based on a specific value but also keep all other values for the unique identifier using pandas?

Viewed 48

What I mean is that let's say we have the following dataframe:

UID  A  
1    Yes  
1    No  
2    No  
2    No  
3    Yes  
3    No  
4    Yes  
4    Yes  

I want to produce a dataframe where any UID that has a yes is included, even the other instances of the UID is a "No".

UID  A  
1    Yes  
1    No  
3    Yes  
3    No  
4    Yes  
4    Yes  

Is there a way to do this using Pandas or any other library in python?

3 Answers

Try with isin

df = df.loc[df.UID.isin(df.loc[df.A=='Yes','UID'])]
df
Out[323]: 
   UID    A
0    1  Yes
1    1   No
4    3  Yes
5    3   No
6    4  Yes
7    4  Yes

I would use a groupby + filter operation:

result = (
    df.groupby('UID')
      .filter(lambda g: g['A'].eq('Yes').any()
)

And that gives me:

   UID    A
0    1  Yes
1    1   No
4    3  Yes
5    3   No
6    4  Yes
7    4  Yes

Try

list = [x for x in df['UID'] if df['A'] == 'Yes']
dfnew = df.query('UID == list & A == ("Yes","No")')

Not elegant but worth a try

Related