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

How can I remove rows containing specific value, on a scalable way?

I have a DataFrame like this:

Column_A    Column_B    Column_C

 10          Total        20
 20           10          20
 30           15          10
 Total        98         Total

I would like to remove all rows, containing ‘Total’.

In my real case, I have many columns. Therefore, I am not looking for this solution:

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

df[df['Column_A'] != 'Total'] 
df[df['Column_B'] != 'Total'] 
df[df['Column_C'] != 'Total'] 

Instead, I would like to have a solution to check for all columns.

The proposed outcome should be:

Column_A    Column_B    Column_C

 20           10          20
 30           15          10

>Solution :

You can use boolean indexing with help of eq, any and the boolean NOT operator ~.

If any value is the row is equal to Total, do not index it.

out = df[~df.eq('Total').any(axis=1)]

output:

  Column_A Column_B Column_C
1       20       10       20
2       30       15       10
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