I'm looking to use something simialr to .isin instead of multiple or's in this code:
def func(row):
if row['Office'] == 'USA' or row['Office'] == 'UK' or row['Office'] == 'Aus' or row['Office'] == 'Can':
return 1
else:
return 2
office_data['Area'] = office_data.apply(func, axis=1)
CodePudding user response:
If using Python 3.10, you can use the match - case
in replace of IF
statements:
def func(row):
match row['Office']:
case 'USA':
return 1
case 'UK':
return 1
case 'Aus':
return 1
case 'Can':
return 1
case _:
return 0
For your case, I would recommend using a dictionary with the possible candidates, like this:
def func(row):
valid_options = ['USA', 'UK', 'Aus', 'Can']
if row['Office'] in valid_options: return 1
return 0