1

I want to delete all rows from my DataFrame that have two True values, like at index 1:

      0      1      2      3      4
0  True  False  False  False  False
1  True  False  False  False   True
2  True  False  False  False  False
3  True  False  False  False  False
4  True  False  False  False  False

How do I do that?

CrazyChucky
  • 3,263
  • 4
  • 11
  • 25
  • 6
    Did you tried something ? – Marius ROBERT Jun 13 '22 at 12:12
  • 4
    Also, please post data/code as [text and not as images](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors-when-asking-a-question). – medium-dimensional Jun 13 '22 at 12:13
  • 1
    We all want something every now and then - but it's those that try to get it that succeed, even if they need a bit of help from SO. [how to ask](https://stackoverflow.com/help/how-to-ask) – Grismar Jun 13 '22 at 12:19
  • 2
    @Grismar Also: [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – Adrien Pacifico Jun 13 '22 at 12:43
  • Well i was planning to try >0 <0 and logic conditions. Data is dummy data. I am trying to optimize for my paper planes. you are great :-) – Mert Açikel Jun 13 '22 at 13:07
  • 2
    As Adrien pointed out, this really is essentially a [duplicate](https://stackoverflow.com/questions/32387524/counting-the-amount-of-true-false-values-in-a-pandas-row). Assuming the original DataFrame is assigned to `df`, all you need is the one-liner `df = df[df.sum(axis=1) == 2]`. (Or `>= 2`, depending on whether you want *exactly* 2 or *at least* 2 `True` values.) Adding in extra conditions and queries and drops just unnecessarily complicates the matter. – CrazyChucky Jun 13 '22 at 14:55
  • 1
    (Sorry, I meant as Pranav pointed out. I read the wrong name.) – CrazyChucky Jun 13 '22 at 15:07
  • 1
    This question is not a duplicate, the other question is not about boolean, and does not refer to counting in its title. 1) You need to think about counting the number of of True per row to solve the problem, but there might be another solution that do not imply counting. Also : https://stackoverflow.blog/2010/11/16/dr-strangedupe-or-how-i-learned-to-stop-worrying-and-love-duplication/ Remember that there is "often benefit to having multiple subtle variants of a question around". – Adrien Pacifico Jun 14 '22 at 07:13
  • 1
    I'm unsure what you mean by "does not refer to counting in its title", since it does. In any event, dupes are not a bad thing! Having this subtle variant *can* certainly be useful to someone, but it may be even more so when the two are linked together. As for them not being the same... Well, the one linked has an extra step to it. If this question is "How do I paint preprimed wood," the other is "How do I prime and paint wood?" The other one's answer *includes* this one's answer, it's just this question *starts* with the true/false values that that one needs generated as a middle step. – CrazyChucky Jun 14 '22 at 09:43
  • So instead of `df[(df > 3).sum(axis=1) >= 3]` as in the linked question, this one just needs `df[df.sum(axis=1) < 2]`. In the linked answer, `(df > 3)` generates an all-boolean DataFrame, a step which isn't needed here. (I misspoke in my earlier comment and dropped the reverse set of rows.) I considered posting that as a community wiki answer, but it's too late now. – CrazyChucky Jun 14 '22 at 09:58
  • @CrazyChucky I absolutely disagree with you. > ""does not refer to counting in its title", since it does" "Pandas not all but two of True" does not contains counting. I personally have to think hard to see the link between those two questions. If you compare both answers they are quite different. Again, I think marking those two questions as duplicate is an error, they are not even the same question, linking one to another will not be very useful for beginner, it also drop the possibility for someone to provide a better answer than mine. It is bad. – Adrien Pacifico Jun 30 '22 at 18:25
  • 1
    @AdrienPacifico I guess I misunderstood, your previous comment looks like you're saying the *other* question doesn't refer to counting in its title. Anyway, if you feel so strongly about it, raise a custom flag and maybe a mod will agree with you. I don't even have enough rep to cast close/reopen votes, myself. – CrazyChucky Jun 30 '22 at 19:39
  • 1
    @AdrienPacifico P.S. If it does get reopened, I'll happily post the solution, `df[df.sum(axis=1) < 2]`, as a [community wiki](https://meta.stackexchange.com/questions/11740/what-are-community-wiki-posts) answer, with credit to [YS-L's answer](https://stackoverflow.com/a/32389030/12975140). – CrazyChucky Jul 02 '22 at 03:17

2 Answers2

1

Generate the dataframe:

import pandas as pd
df = pd.DataFrame({
    "0": [True, True, True, True, True],
    "1": [False, False, False, False, False],
    "2": [False, False, False, False, False],
    "3": [False, False, False, False, False],
    "4": [False, True, False, False, False]

})

Get the number of true per row:

df = df.assign(number_of_true= lambda x: x.sum(axis=1))

      0      1      2      3      4  number_of_true
0  True  False  False  False  False               1
1  True  False  False  False   True               2
2  True  False  False  False  False               1
3  True  False  False  False  False               1
4  True  False  False  False  False               1

Select row that do not have two True on one row:

df = df.query("number_of_true != 2")

One liner:

(
df
.assign(number_of_true= lambda x: x.sum(axis=1))
.query("number_of_true != 2")
.drop(columns="number_of_true") #clean dataframe
)
Output:
      0      1      2      3      4
0  True  False  False  False  False
2  True  False  False  False  False
3  True  False  False  False  False
4  True  False  False  False  False

Adrien Pacifico
  • 1,649
  • 1
  • 15
  • 33
0

one approach can be to count True occurences and then drop rows on basis of count like so: df['count'] = df[['1', '2']].sum(axis=1) then dropping like so: df3 = df[df['count'] > 2]

i hope i didn't solve your homework xD

Atlas Bravoos
  • 360
  • 2
  • 12