-3

Currently, I have a list of Date and time (2017-12-13 01:05:44) in my dataframe.
Can anyone please help me to create a new column ('Day') in my dataframe, with a list of days corresponding to my date columns.

*Can Ignore the time.

For example, the output:

Date, Day
2017-12-13 01:05:44, Wednesday  
2017-12-12 01:02:13, Tuesday
2017-12-11 01:00:55, Monday
2017-12-10 00:59:31, Sunday
2017-12-09 00:58:23, Saturday

enter image description here

123cremepie
  • 339
  • 1
  • 3
  • 5
  • 1
    Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) before attempting to ask more questions because this is not a good question. This is not a code writing service, nor a read the docs and re-type them here for me service. –  Dec 13 '17 at 02:01
  • Possible duplicate of [how to get day name in datetime in python](https://stackoverflow.com/questions/8380389/how-to-get-day-name-in-datetime-in-python) – Dewald Abrie Dec 13 '17 at 02:06
  • I'm voting to close this question as off-topic because "gimme teh codez" / "do my work for me." – EJoshuaS - Stand with Ukraine Dec 16 '17 at 05:23

1 Answers1

1

If you have date as strings then you have to convert them to datetime object

df['Date'] = pd.to_datetime(df['Date'])

and layter you can use Series.dt.weekday_name

df['Day'] = df['Date'].dt.weekday_name

See: timeseries - time date components and pandas.to_datetime()


Working example

import pandas as pd

data = [
    "2017-12-13 01:05:44",
    "2017-12-12 01:02:13",
    "2017-12-11 01:00:55",
    "2017-12-10 00:59:31",
    "2017-12-09 00:58:23",
]

# create dataframe
df = pd.DataFrame(data, columns=['Date'])

print('\ntype before:\n', df.dtypes)

# convert strings to datetime
df['Date'] = pd.to_datetime(df['Date'])

print('\ntype after:\n', df.dtypes)

# get weekday name
df['Day'] = df['Date'].dt.weekday_name

print(df)

Result:

type before:
 Date    object
dtype: object

type after:
 Date    datetime64[ns]
dtype: object
                 Date        Day
0 2017-12-13 01:05:44  Wednesday
1 2017-12-12 01:02:13    Tuesday
2 2017-12-11 01:00:55     Monday
3 2017-12-10 00:59:31     Sunday
4 2017-12-09 00:58:23   Saturday
furas
  • 134,197
  • 12
  • 106
  • 148