2

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.

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

5 Comments

if the holiday is also a weekday, then the holiday should take precedence. I wonder if that is possible with your code?
It is hard test without data, but in my opinion there can be precedence. Trues in m1 with Trues in m2, ...
I add sample data, it works very nice - it replace with precedence.
I am actually new to this place.I did that right now.Once again really thanks
Very nice. How can I do it for the same condition in only one column? Say m1,m2,m3 on column B alone?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.