1

Wanting to convert in my dataframe with this format df['Start Date'] 2013-12-02 and I want to create a column where its tells me the name of day of the week according to the start date.

def with_weekday(dataframe, date_field):
    
    dataframe = dataframe.copy(deep=True)
    
    dataframe ['Weekday'] = day_name(dataframe['Start Date']) #help here
    
    return dataframe
   

#the 'Start Date' is the column with dates that I want to convert with this format2013-12-02
  • So you're trying to convert the `date_field` parameter from `with_weekday` method to the format `year-month-day` in Python? – Cooper Scott Nov 24 '20 at 03:11
  • I no, I am trying to create a new colum where it treansform the column Start Date data which is 2013-12-02 to day of the week (Tuesday) – StackSuperLow Nov 24 '20 at 03:48
  • 1
    Try this https://stackoverflow.com/questions/30222533/create-a-day-of-week-column-in-a-pandas-dataframe-using-python – Joe Ferndz Nov 24 '20 at 05:53
  • `df['Weekday'] = df['Start Date'].dt.day_name()` – Joe Ferndz Nov 24 '20 at 05:55
  • Does this answer your question? [Create a day-of-week column in a Pandas dataframe using Python](https://stackoverflow.com/questions/30222533/create-a-day-of-week-column-in-a-pandas-dataframe-using-python) – Joe Ferndz Nov 24 '20 at 05:56

2 Answers2

0

For day name you can write a simple method

def get_day_name(date):
    day_name= ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday','Sunday']
    day = datetime.datetime. strptime(date, '%d %m %Y'). weekday()
    return day_name[day]

df['day_name'] = df['start_date'].apply(lambda x: get_day_name(x))
ATIF ADIB
  • 571
  • 5
  • 10
0

You can do it like this

df['day_name'] = pd.to_datetime(df['start_date']).dt.day_name()
Hasnat
  • 531
  • 4
  • 8