user8183395
user8183395

Reputation: 115

Create a Pandas Dataframe column based on multiple condition

I am trying to create a new column based on the multiple conditions shown in my code. I have a dictionary for jp_hol which has the holidays in japan and my dataframe has the that date column which is a string, and all other columns used in the function I however get this error below could someone help me figure out the problem

The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

my code:

def flag():
    if (load['date'].isin([i for i in jp_hol.keys()]) |(load['day_of_week_int']==6)):
         l='holiday'
    elif load['day_of_week_int'].isin([i for i in range(0,5)]): 
         l='weekday'
    elif load['day_of_week_int']==5:
         l='sat'
return l
load['flag']=load.apply(flag(),axis=1

Note: if the holiday falls in a weekday then the holiday should take precedence over weekday.

Upvotes: 2

Views: 5017

Answers (1)

jezrael
jezrael

Reputation: 862406

All mask create True and False Series, so is possible use numpy.where:

m1 = load['date'].isin([i for i in jp_hol.keys()]) | (load['day_of_week_int']==6)
m2 = load['day_of_week_int'].isin([i for i in range(0,5)])
m3 = load['day_of_week_int']==5


load['flag']=np.where(m1, 'holiday',
             np.where(m2, 'weekday',
             np.where(m3, 'sate', 'no match')))

Sample:

load = pd.DataFrame({'A':list('abcdef'),
                   'B':[4,5,4,5,5,4],
                   'C':[7,8,9,4,2,3],
                   'D':[1,3,5,7,1,0],
                   'E':[5,3,6,9,2,4],
                   'F':list('aaabbb')})

print (load)
m1 = load['B'] == 5
m2 = load['C'] >5
m3 = load['F'] == 'a'

print (pd.concat([m1,m2,m3], axis=1))
       B      C      F
0  False   True   True
1   True   True   True
2  False   True   True
3   True  False  False
4   True  False  False
5  False  False  False

load['flag']=np.where(m1, 'holiday',
             np.where(m2, 'weekday',
             np.where(m3, 'sate', 'no match')))

print (load)
   A  B  C  D  E  F      flag
0  a  4  7  1  5  a   weekday
1  b  5  8  3  3  a   holiday
2  c  4  9  5  6  a   weekday
3  d  5  4  7  9  b   holiday
4  e  5  2  1  2  b   holiday
5  f  4  3  0  4  b  no match

Upvotes: 2

Related Questions