Home > Software design >  How can I remove rows containing specific value, on a scalable way?
How can I remove rows containing specific value, on a scalable way?

Time:06-28

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:

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

CodePudding user response:

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

CodePudding user response:

This is the another solution but this is not much optimal for larger data.

for column in df.columns:
   df = df[df[column]!='Total']
  • Related