Используйте df.apply с axis = 1 для применения функции к вашему фрейму данных построчно.
import pandas as pd
# create some random data
df = pd.DataFrame({'code': ['Vs', 'vx', 'Vxyz'], 'event_date': ['01-30-20', '03-01-20', None]})
# we should expect that only row 1 --> label = 1
# the code for row 2 doesn't begin with 'V' --> label = 0
# and code for row 3 has None for the date --> label = 0
def set_value(x):
if x['code'][0] == 'V' and x['event_date'] != None:
return 1
else:
return 0
# apply the function to each row of the df
df['label'] = df.apply(lambda x: set_value(x), axis = 1)
Вывод:
>>> df
code event_date label
0 Vs 01-30-20 1
1 vx 03-01-20 0
2 Vxyz None 0