Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to count the number of rows that have the value 1 for all the columns in a dataframe?

The following is an example dataframe for this issue:

name    gender    address    phone no.
---------------------------------------
1       1         1          1
1       0         0          0
1       1         1          1
1       1         0          1

The desired output here is 2 because the number of rows containing all 1s is 2.

Can anyone please help me with this issue?
Thanks.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use eq(1) to identify the values with 1, then aggregate per row with any to have True when all values are True and sum the True taking advantage of the True/1 equivalence:

df.eq(1).all(axis=1).sum()

output: 2

Intermediates:

df.eq(1)
   name  gender  address  phone no.
0  True    True     True       True
1  True   False    False      False
2  True    True     True       True
3  True    True    False       True

df.eq(1).all(axis=1)
0     True
1    False
2     True
3    False
dtype: bool
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading