Python Forum
Adding a new column to a dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding a new column to a dataframe
#1
Hello,

I am working with covid-19 data, and I am trying to add a new column to the dataframe based on the datetime, converting it to a day of the week.

import pandas as pd

url='https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv'
data = pd.read_csv(url)
data.head()
The output of that looks like this:
Output:
date state fips cases deaths 0 2020-01-21 Washington 53 1 0 1 2020-01-22 Washington 53 1 0 2 2020-01-23 Washington 53 1 0 3 2020-01-24 Illinois 17 1 0 4 2020-01-24 Washington 53 1 0
What I want to do, is to add a column called 'dow' that converts the date into the actual day of week.

So I tried this:

data['DOW'] = datetime.date(data['date']).strftime('%A')
But that is giving me this error:

Error:
TypeError Traceback (most recent call last) <ipython-input-33-ae85c40abf8d> in <module>() 1 data['DOW'] = 'Day' ----> 2 print(datetime.date(data['date']).strftime('%A')) /usr/local/lib/python3.6/dist-packages/pandas/core/series.py in wrapper(self) 127 if len(self) == 1: 128 return converter(self.iloc[0]) --> 129 raise TypeError(f"cannot convert the series to {converter}") 130 131 wrapper.__name__ = f"__{converter.__name__}__" TypeError: cannot convert the series to <class 'int'>
I would appreciate any help!
Reply
#2
import pandas as pd
 
url='https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv'
data = pd.read_csv(url, parse_dates=['date'])
data['DOW'] = data['date'].apply(lambda x: x.strftime('%A'))

# if you don't want to convert date columne:
# data = pd.read_csv(url)
# data['DOW'] = pd.to_datetime(data['date']).apply(lambda x: x.strftime('%A'))

print(data.head())
Output:
date state fips cases deaths DOW 0 2020-01-21 Washington 53 1 0 Tuesday 1 2020-01-22 Washington 53 1 0 Wednesday 2 2020-01-23 Washington 53 1 0 Thursday 3 2020-01-24 Illinois 17 1 0 Friday 4 2020-01-24 Washington 53 1 0 Friday
In the example I convert date column to datetime object when I read the csv file. In the comments - example if you do not want to convert it
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Also
data['DOW'] = data['date'].dt.strftime('%A')
or

data['DOW'] = data['date'].dt.day_name()
where date columns is already converted to datetime
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding PD DataFrame column bsben 2 245 Mar-08-2024, 10:46 PM
Last Post: deanhystad
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 690 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Difference one column in a dataframe Scott 0 619 Feb-10-2023, 08:41 AM
Last Post: Scott
  splitting a Dataframe Column in two parts nafshar 2 912 Jan-30-2023, 01:19 PM
Last Post: nafshar
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 798 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  renaming the 0 column in a dataframe Led_Zeppelin 5 1,460 Aug-16-2022, 04:07 PM
Last Post: deanhystad
  Copy a column from one dataframe to another dataframe Led_Zeppelin 17 10,991 Jul-08-2022, 08:40 PM
Last Post: deanhystad
  Cannot convert the series to <class 'int'> when trying to create new dataframe column Mark17 3 8,389 Jan-20-2022, 05:15 PM
Last Post: deanhystad
  Filter dataframe by datetime.date column glidecode 2 4,916 Dec-05-2021, 12:51 AM
Last Post: glidecode
  Adding another condition for df column comparison Mark17 2 1,610 Sep-30-2021, 03:54 PM
Last Post: Mark17

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020