Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dataframe drop rows where multiple columns have the same value

My dataframe has the columns A, B, C, label1, label2, label3. I just want to drop the rows where label1 = label2 = label3. The label value can be 0, 1, 2, 3 and nan
The best solution I’ve found so far is this

df = df.drop(df[(df['label1'] == df['label2']) & (df['label1'] == df['label3'])].index)

Is there any other way I could solve this problem since the code above feels wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

For solution for working with multiple columns by list is possible filter them first and then compare if not equal all filtered values in df1 by first column with DataFrame.any for filter all rows if not same values – it is same like drop rows with same values:

print (df)
   A  B  C  label1  label2  label3
0  4  5  6       0       0       0
1  1  2  3       7       4       5

df1 = df[['label1','label2', 'label3']]

df = df[df1.ne(df1.iloc[:, 0], axis=0).any(axis=1)]
print (df)
   A  B  C  label1  label2  label3
1  1  2  3       7       4       5
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading