lacerda
lacerda

Reputation: 1

Adding to a dataframe a new column

Start_Time
2016-02-08 05:46:00
2016-02-08 06:07:59
2016-02-08 06:49:27
2016-02-08 07:23:34

This is a column of a dataframe I am working with. How can I add to the dataframe an extra column that holds the day of the week that the Start_Time occurred in each line?

Upvotes: 0

Views: 41

Answers (1)

Umar.H
Umar.H

Reputation: 23099

assuming your column is already a datetime we can use the dt. accessor to use datetime methods

if not use pd.to_datetime(df['Start_Time'])

df['day_name'] = df['Start_Time'].dt.day_name()

or

df['dayofweek'] = df['Start_Time'].dt.dayofweek

           Start_Time day_name  dayofweek
0 2016-02-08 05:46:00   Monday          0
1 2016-02-08 06:07:59   Monday          0
2 2016-02-08 06:49:27   Monday          0
3 2016-02-08 07:23:34   Monday          0

Upvotes: 1

Related Questions