Issue
I'm trying to write as list comprehension the following code:
is_weekend = list()
for i in df['date']:
if i.weekday() > 4:
is_weekend.append(1)
else:
is_weekend.append(0)
I've already tried
is_weekend = [i == 1 for i in df['date'] if i.weekday() > 4 else i == 0]
But it throws invalid syntax errors.
Could you help?
Solution
This is the correct way:
is_weekend = [1 if i.weekday() > 4 else 0 for i in df['date']]
Answered By - Riccardo Bucco
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.