2

I a m working on this Data Set I want to get cumulative confirmed cases so I filtered by confirmed cases, grouped by Date and aggregated by sum.

import matplotlib.pyplot as plt
covid_tab=pd.read_csv('datasets/COVID-19 Cases.csv')

covid_tab['Date']=pd.to_datetime(covid_tab['Date'])
covid_tab.groupby(["Country_Region","Case_Type"]).agg({'Cases':'max'}).head()
cumulative_cases=covid_tab[covid_tab['Case_Type']=='Confirmed'].groupby('Date').agg({'Cases': 'sum'})
cumulative_cases.head()

I get something like this

what i get

but if I try to access the Date column I get a Key error, or if I try to print the column names I only get 'Cases' printed

Why is that?

gloria00
  • 265
  • 2
  • 15

1 Answers1

3

Date is set as an index after the groupby method. You can use reset_index() to turn it into a column

covid_tab[covid_tab['Case_Type']=='Confirmed'].groupby('Date').agg({'Cases': 'sum'}).reset_index()
Erfan
  • 40,971
  • 8
  • 66
  • 78
dp6000
  • 473
  • 5
  • 15