I have a binairy dataframe and I would like to check whether all values in a specific row have the value 1. So for example I have below dataframe. Since row 0 and row 2 all contain value 1 in col1 till col3 the outcome shoud be 1, if they are not it should be 0.
import pandas as pd
d = {'col1': [1, 0,1,0], 'col2': [1, 0,1, 1], 'col3': [1,0,1,1], 'outcome': [1,0,1,0]}
df = pd.DataFrame(data=d)
Since my own dataframe is much larger I am looking for a more elegant way than the following, any thoughts?
def similar(x):
if x['col1'] == 1 and x['col2'] == 1 and x['col3'] == 1:
return 1
else:
''
df['outcome'] = df.apply(similar, axis=1)