I assume you are using Pandas (due to the 'df' notation). If so, you can assign col3 a boolean flag using .gt (more) to compare col2 with zero. Multiplying the result by one converts the Boolean flags to ones and zeros.
df1 = pd.DataFrame({'col1': [1, 0, 0, 0, 3, 2, 0], 'col2': [0, 1, 0, 0, 3, 0, 4]}) df1['col3'] = df1.col2.gt(0) * 1 >>> df1 Out[70]: col1 col2 col3 0 1 0 0 1 0 1 1 2 0 0 0 3 0 0 0 4 3 3 1 5 2 0 0 6 0 4 1
You can also use the lambda expression to achieve the same result, but I find the above method easier for your given example.
df1['col3'] = df1['col2'].apply(lambda x: 1 if x > 0 else 0)
source share