How to add logical AND
in a control statement involving two columns of a pandas dataframe ie
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')
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.