Home > Back-end >  Remove rows in pandas dataframe if any of specific columns contains a specific value
Remove rows in pandas dataframe if any of specific columns contains a specific value

Time:06-01

I have the following df:

enter image description here

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!

CodePudding user response:

Your question was not clear. Did you mean that a row need to be delete if pump.test < 95 or feed.test < 95?

In that case don't delete or remove just do it the other way around und do a positive selection. Select all rows where feed.test and pump.test is equal or greater then 95.

df.loc[df['feed.test'].ge(95) & df['pump.test'].ge(95)]

CodePudding user response:

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
  • Related