Reputation: 35
I need to create a boolean column in pandas such that odd days will give me True, and even days will give me False.
The dates are in the format of 2019-MM-DD, such as 2019-06-04.
2019-06-04 is an even day, so I want to get False from it 2019-06-03 is an odd day, so I want to get True from it
I was thinking something along the lines of
df['New Column'] = int(df['Date'].astype(str).iloc[0][8:]) % 2 == 1
But the code above will only return the boolean of the first row in ['Date']
How do I make it such that the index 0 will increment down the rows?
Upvotes: 2
Views: 532
Reputation: 24314
try via day
attribute:
df['Date']=pd.to_datetime(df['Date'])
#ensure that 'Date' column is of dtype datetime
df['New Column'] =df['Date'].dt.day%2!=0
#Finally check if the day is even or not
Upvotes: 3