I am trying to come up with a way to assign values to dates in my panda's data frame. I currently have a list of dates grouped by "state" as shown in the table. I would like to have "03-01-2020" be "day 1" and the current day be the last day-- in a column called "day". I tried using this code to create the dictionary I could use in the Pandas dataframe with the code below:
counter = 0
dict1 = {}
for i in df.date:
if i not in dict1:
counter +=1
dict1[i] = counter
dict1
This code was to assign a number to a date. I then tried the following code to match the date column with its corresponding number. I tried putting it in a list so I can eventually make a column with this list and append it to the data frame
list1 = []
for x in df.date:
for key,value in enumerate(dict1):
if value == x:
list1.append(key)
print(list1)
However, when I tried the code I got a large list just iterating 0 to 360 everytime. This is not what I wanted. I wanted that if a state (remember I grouped the df by state) started recording data at different days the number would correspond with the date.
Ex: if Texas started recording at 03-01-2020 it would be 1st day in the "Day" Column However, if Arizona started recording at 03-05-2020, it would be the 5th day in "Day" Column
Data Frame Looks like this:
Date State Cases
2020-03-01 AK 15.5000
2020-03-02 AK 28.4048
2020-03-03 AK 43.8333
2020-03-04 AK 60.8905
2020-03-05 AK 81.3548