Abhishek Gowda S
Abhishek Gowda S

Reputation: 25

how to update a column containing date(yyyy-mm-dd) into weekday(wd) or weekend(we) in python using pandas

i have a CSV file which contains userid and other user related data in columns and one of which is date , now i want to convert this date column to whether that date is weekend(we) or weekday(wd) which helps me in some Machine learning training ,thank you .

Upvotes: 1

Views: 66

Answers (1)

jezrael
jezrael

Reputation: 862921

I think you need numpy.where with dayofweek:

df['day'] = np.where(df['Date'].dt.dayofweek > 4, 'we', 'wd')

Sample:

rng = pd.date_range('2017-04-03', periods=10)
df = pd.DataFrame({'Date': rng, 'a': range(10)})  

df['day'] = np.where(df['Date'].dt.dayofweek > 4, 'we', 'wd')
print (df)
        Date  a day
0 2017-04-03  0  wd
1 2017-04-04  1  wd
2 2017-04-05  2  wd
3 2017-04-06  3  wd
4 2017-04-07  4  wd
5 2017-04-08  5  we
6 2017-04-09  6  we
7 2017-04-10  7  wd
8 2017-04-11  8  wd
9 2017-04-12  9  wd

If need overwrite column:

df['Date'] = np.where(df['Date'].dt.dayofweek > 4, 'we', 'wd')
print (df)
  Date  a
0   wd  0
1   wd  1
2   wd  2
3   wd  3
4   wd  4
5   we  5
6   we  6
7   wd  7
8   wd  8
9   wd  9

For convert to datetime in read_csv use parameter parse_dates:

import pandas as pd
from pandas.compat import StringIO

temp=u"""datetime,YEAR
2016-01-19,2016.0
2016-01-20,2016.0
2016-01-21,2016.0
2016-01-22,2016.0
2016-01-23,2016.0
2017-02-02,2017.0"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp),  parse_dates=[0])

print (df)
    datetime    YEAR
0 2016-01-19  2016.0
1 2016-01-20  2016.0
2 2016-01-21  2016.0
3 2016-01-22  2016.0
4 2016-01-23  2016.0
5 2017-02-02  2017.0

print (df.dtypes)
datetime    datetime64[ns]
YEAR               float64
dtype: object

Upvotes: 1

Related Questions