I have daily time series data by different counties in US, which is covid cases daily time series, I want to restructure/reshape it in order to use it with other time series data that I have. So I tried groupby operation to regroup the time series, but I got value error as follow:
ValueError: No axis named county_state for object type DataFrame
I am not sure using groupby is the right move to take. Can anyone suggest possible way of doing this right in pandas? Any idea?
current attempt
Here is the reproducible data on gist. Here is the my current attempt:
import pandas as pd
df = pd.read_csv("df.csv")
df['date'] = pd.to_datetime(df['date'])
df.groupby('date', 'county_state')['cases', 'deaths'].unstack().reset_index()
but above attempt is not working, which cause ValueError instead. Can anyone suggest how to make this right?
desired output
Here is structure of output of my expected dataframe, no need to use aggregate by cases or deaths.
date fips cases deaths county_state
1/26/2020 4013 1 0 Maricopa_Arizona
1/27/2020 4013 5 0 Maricopa_Arizona
1/28/2020 4013 7 0 Maricopa_Arizona
...
9/02/202 4013 2333 100 Maricopa_Arizona
1/26/2020 6037 1 0 Los Angeles_California
1/27/2020 6037 15 2 Los Angeles_California
1/28/2020 6037 20 4 Los Angeles_California
...
9/02/202 6037 10001 200 Los Angeles_California
How to achieve my expected output above? Any way to get this right in pandas?