Reputation: 4571
How to add logical AND
in a control statement involving two columns of a pandas dataframe i.e.
This works:
def getContinent(row):
if row['Location'] in ['US','Canada']:
val = 'North America'
elif row['Location'] in['UK', 'Germany']:
val = 'Europe'
else:
val = None
return val
df.apply(getContinent, axis=1)
Now I want to include an additional condition with another field row['Sales']
:
def getContinent(row):
if row['Location'] in ['US','Canada'] & row['Sales'] >= 100:
val = 'North America'
elif row['Location'] in['UK', 'Germany'] & row['Sales'] < 100:
val = 'Europe'
else:
val = None
return val
df.apply(getContinent, axis=1)
ValueError: ('Arrays were different lengths: 6132 vs 2', u'occurred at index 0')
Upvotes: 1
Views: 5411
Reputation: 863651
You need use and
instead &
:
df = pd.DataFrame({'Sales': {0: 400, 1: 20, 2: 300},
'Location': {0: 'US', 1: 'UK', 2: 'Slovakia'}})
print (df)
Location Sales
0 US 400
1 UK 20
2 Slovakia 300
def getContinent(row):
if row['Location'] in ['US','Canada'] and row['Sales'] >= 100:
val = 'North America'
elif row['Location'] in['UK', 'Germany'] and row['Sales'] < 100:
val = 'Europe'
else:
val = None
return val
print (df.apply(getContinent, axis=1))
0 North America
1 Europe
2 None
dtype: object
Upvotes: 4