1

enter image description here

enter image description here

I try to create a new column with the day of the week:

df2019['Weekday']=pd.to_datetime(df2019['Year'],df2019['Month'],df2019['Day']).weekday()

And I get the following error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Thanks!

Nowshin
  • 81
  • 2
  • 10
Miguel Gonzalez
  • 398
  • 1
  • 12

2 Answers2

3

You can do something like this:

from datetime import datetime

def get_weekday(row):
    date_str = "{}-{}-{}".format(row["Year"], row["Month"], row["Day"])
    date = datetime.strptime(date_str, '%Y-%m-%d')
    return date.weekday()


df2019["weekday"] = df2019.apply(get_weekday, axis=1)
Anwarvic
  • 12,156
  • 4
  • 49
  • 69
2

Since your Timestamp is already of datetime format, you can do this:

df2019['weekday'] = df2019['Timestamp'].dt.weekday
Partha Mandal
  • 1,391
  • 8
  • 14