Home > Enterprise >  Find the rows that share the value
Find the rows that share the value

Time:01-21

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

CodePudding user response:

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

CodePudding user response:

This is what you are looking for:

df1["TRUE"] = (df1==1).all(axis=1).astype(int)
  •  Tags:  
  • Related