-1

I have downloaded the data from here: https://finance.yahoo.com/quote/%5ENDX/history/

I want to add a weekday column in the dataframe which will contain the day of the week for that particular day.

#Sample of data that I am working with:

      Date         Open        High        Low        Close      Adj Close   Volume
0   1985-10-01  110.620003  112.160004  110.565002  112.139999  112.139999  153160000
1   1985-10-02  112.139999  112.540001  110.779999  110.824997  110.824997  164640000
2   1985-10-03  110.839996  111.184998  110.120003  110.870003  110.870003  147300000
3   1985-10-04  110.870003  110.870003  109.855003  110.074997  110.074997  147900000
4   1985-10-07  110.074997  110.135002  108.175003  108.199997  108.199997  128640000

1 Answers1

1

You can use .dt.dayofweek() to get the day of week with Monday=0, Sunday=6:

df['Date'] = pd.to_datetime(df['Date'])     # skip if your Date column already in datetime format
df['Day of Week'] = df['Date'].dt.dayofweek

Result:

print(df)


        Date        Open        High         Low       Close   Adj Close     Volume  Day of Week
0 1985-10-01  110.620003  112.160004  110.565002  112.139999  112.139999  153160000            1
1 1985-10-02  112.139999  112.540001  110.779999  110.824997  110.824997  164640000            2
2 1985-10-03  110.839996  111.184998  110.120003  110.870003  110.870003  147300000            3
3 1985-10-04  110.870003  110.870003  109.855003  110.074997  110.074997  147900000            4
4 1985-10-07  110.074997  110.135002  108.175003  108.199997  108.199997  128640000            0

If you want the day of week in text, you can use .dt.day_name(), as follows:

df['Day of Week Text'] = df['Date'].dt.day_name()

Result:

        Date        Open        High         Low       Close   Adj Close     Volume  Day of Week Day of Week Text
0 1985-10-01  110.620003  112.160004  110.565002  112.139999  112.139999  153160000            1          Tuesday
1 1985-10-02  112.139999  112.540001  110.779999  110.824997  110.824997  164640000            2        Wednesday
2 1985-10-03  110.839996  111.184998  110.120003  110.870003  110.870003  147300000            3         Thursday
3 1985-10-04  110.870003  110.870003  109.855003  110.074997  110.074997  147900000            4           Friday
4 1985-10-07  110.074997  110.135002  108.175003  108.199997  108.199997  128640000            0           Monday
SeaBean
  • 22,547
  • 3
  • 13
  • 25