Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert dataframe date row to a weekend / not weekend value

I have a dataframe with a DATE row and I need to convert it to a row of value 1 if the date is a weekend day and 0 if it is not.

So far, I converted the data to weekdays

df['WEEKDAY'] = pandas.to_datetime(df['DATE']).dt.dayofweek

It's there a way to create this "WEEKEND" row without functions?

Thanks!

like image 801
Raul Avatar asked Aug 28 '15 19:08

Raul


People also ask

How do you get the date difference in pandas?

Use df. dates1-df. dates2 to find the difference between the two dates and then convert the result in the form of months.

How do I work with dates and times in pandas?

Pandas has a built-in function called to_datetime()that converts date and time in string format to a DateTime object. As you can see, the 'date' column in the DataFrame is currently of a string-type object. Thus, to_datetime() converts the column to a series of the appropriate datetime64 dtype.


1 Answers

One more way of getting weekend indicator is by where function:

df['WEEKDAY'] = np.where((df['DATE']).dt.dayofweek) < 5,0,1)
like image 137
Daniel Ben-David Avatar answered Sep 20 '22 12:09

Daniel Ben-David