Pedro Sismeiro
Pedro Sismeiro

Reputation: 41

How can I make a new column in a dataframe, with values 0 or 1 whether it was weekend or business day?

I have a DataFrame with a DatetimeIndex and I need to get a new column with values 0 (for a Saturday or Sunday) or 1 (if it was business day) based on the datetime index. How can I do it in a way like:

keytable['var']= if 'Saturday' or 'Sunday' == 0 else return 1

Thanks in advance for the amazing support this community gives to coders worldwide!

Upvotes: 0

Views: 25

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150775

You can do:

keytable['var'] = (keytable.index.weekday < 5).astype(int)

Upvotes: 2

Related Questions