Skip to content
Advertisement

Remove rows in pandas dataframe if any of specific columns contains a specific value

I have the following df:

Data Frame

I have not been able to figure out how to delete a row if any of the columns containing the word “test” is less than 95. For example, I would have to delete the entire index row 1 because the column “heat.test” is 80 (the same for rows 0 and 3). In other words, if only one column meets this condition, the whole row must be deleted.

Thank you!

Advertisement

Answer

I think this is what you’re asking:

df[~(df.le(95) & df.columns.str.contains("test"))].dropna()

Example (df):

    pump.test  Speed  feed.test  water
0   100        1000   70         0.2
1   100        2000   100        0.3
2   100        3000   100        0.4
3   95         4000   100        0.5

Output of the operation above:

    pump.test  Speed  feed.test  water
1   100        2000   100        0.3
2   100        3000   100        0.4
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement