0

What I am trying to figure out is how to add "Cases" and "Deaths" for each day, so that it starts with: "1/19/2020 Cases" and "1/19/2020 Deaths" then "1/20/2020 Cases" etc. It seems the append function does not work for this, and I don't know how else to add this. It doesn't seem like python has a way to do this task. My eventual goal is to make this a pandas dataframe.

import pandas as pd

dates = pd.date_range(start = '1/19/2020', end = '12/31/2021')

lst = dates.repeat(repeats = 2)

print(lst)

Thanks

Colby
  • 7
  • 1
  • 5

1 Answers1

0

If I am not mistaken, I don't think there's a way to do it with purely pandas. However with python and datetime, you can do so:

import pandas as pd
from datetime import timedelta, date

def daterange(start_date, end_date):
    # Credit: https://stackoverflow.com/a/1060330/10640517
    for n in range(int((end_date - start_date).days)):
        yield start_date + timedelta(n)

dates = []
start_date = date(2020, 1, 19) # Start date here
end_date = date(2021, 12, 31) # End date here
for single_date in daterange(start_date, end_date):
    dates.append(single_date.strftime("%m/%d/%Y") + " Cases")
    dates.append(single_date.strftime("%m/%d/%Y") + " Deaths")

pdates = pd.DataFrame(dates)

print (pdates)

Is this what you want? If not, I can delete it.

Gareth Ma
  • 707
  • 4
  • 10