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

Find the rows that share the value

I need to find where the rows in ABC all have the value 1 and then create a new column that has the the rusult.

my idea is to use np.where() with some condition, but i dont know the correct way of dealing with this problem, from what i have read im not suposed to iterate through a dataframe, but use some of pandas creative methods?

df1 = pd.DataFrame({'A': [0, 1, 1, 0],
                    'B': [1, 1, 0, 1],
                    'C': [0, 1, 1, 1],},
                        index=[0, 1, 2, 4])
print(df1)


what i am after is this:

   A  B  C TRUE
0  0  1  0  0
1  1  1  1  1   <----
2  1  0  1  0
4  0  1  1  0

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 :

If the data is always 0/1, you can simply take the product per row:

df1['TRUE'] = df1.prod(1)

output:

   A  B  C  TRUE
0  0  1  0     0
1  1  1  1     1
2  1  0  1     0
4  0  1  1     0
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