Home > Software design >  Filter a dataframe using two conditions in python
Filter a dataframe using two conditions in python

Time:10-29

I want to filter a dataframe using two different condition. But i want to omit rows which doesn't satisfy the condition and only want to keep values which occur at least 2 times in coloumn A

so this is the sample data

df1 = df[(df['A-B occurrence'] >= 3) & (df['A occurrence'] >= 2)]

above is the code iam using and this is the ouput

enter image description here

so as in coloumn A, 17 is satisfying condition in one row only so i want to omit 17 all together as it is not meeting the condition, which means i only want to keep duplicate values which are present in coloumn A 2 or more than 2 times

CodePudding user response:

IIUC you want to keep only the rows for which A has duplicates.

You can use:

df2 = df1[df1['A'].duplicated(keep=False)]

output: this should remove rows with index 14 (A=17) and 19 (A=19)

NB. you can apply the same strategy on the other columns if needed

  • Related