I am trying to create a rule where as long as the sum of all data across each individual row in the dataframe is greater than one, the response will equal to one. Please see below.
import numpy as np import pandas as pd df1 = pd.DataFrame(np.random.randint(0,2,size=(10, 4)), columns=list('ABCD')) df1['Response'] = 0 df1 Out[14]: A B C D Response 0 0 0 0 0 0 1 0 1 1 0 0 2 1 1 1 1 0 3 0 0 0 0 0 4 0 1 1 1 0 5 1 1 0 0 0 6 1 1 0 0 0 7 0 1 1 1 0 8 0 0 0 0 0 9 0 1 1 1 0 My attempt:
df1['Response'] = 1 if [sum(df1[i,:]) for i in range(10)] > 1 else 0 However I get this error, instead of having three rows equal to zero and the remaining equal to 1 in the response column:
TypeError: unhashable type: 'slice' Any help would be appreciated. Thank you.
df1['Response'] = df1.sum(1).gt(1).astype(int)