1

suppose I have a dataframe in the form of:

a   b   c  
89   9   2  
90   5   5  
10  70  20  
25  50  25

my goal is to remove the rows where 100 minus the sum of these three columns is greater than 0.5.

how can I do that?

  • Here are a few ways for you to filter out the rows from a [dataframe](https://www.listendata.com/2019/07/how-to-filter-pandas-dataframe.html), and on [Stack Overflow](https://stackoverflow.com/questions/11869910/pandas-filter-rows-of-dataframe-with-operator-chaining) – Joe Ferndz Mar 30 '21 at 21:51
  • Does this answer your question? [How to select rows from a DataFrame based on column values](https://stackoverflow.com/questions/17071871/how-to-select-rows-from-a-dataframe-based-on-column-values) – Henry Ecker Mar 30 '21 at 21:54
  • I have added my solution as an answer. please check it out and see if it is a valid method. – Alirezaro93 Mar 30 '21 at 22:01

2 Answers2

4

One solution:

(100 - df.sum(1)).le(0.5)

This is a boolean mask that you can give it to a dataframe:

df[(100 - df.sum(1)).le(0.5)]
ashkangh
  • 1,594
  • 1
  • 6
  • 9
0

Well here is what I came up with and it works just fine:

indexNames = df[((100 - df1['a'] - df['b'] - df['c']).abs() > 0.5)].index

df.drop(indexNames , inplace=True)
  • in the method that I have proposed, it can take care of any dataframe and it also considers absolute vale of the result so it would be more robust – Alirezaro93 Mar 30 '21 at 22:04