jeudi 18 août 2016

Pandas dataframe If else with logical AND involving two columns

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')

Aucun commentaire:

Enregistrer un commentaire