Home > OS >  Convert summary data (cumulative cases) to daily cases pandas
Convert summary data (cumulative cases) to daily cases pandas

Time:03-03

I have case data that is presented as a time series. They are summed for each following day, what can be used to turn them into daily case count data?

My dataframe in pandas:

    data         sum_cases (cumulative)
0   2020-05-02   4.0
1   2020-05-03   21.0
2   2020-05-04   37.0
3   2020-05-05   51.0

I want them to look like this:

    data         sum_cases(cumulative)    daily_cases
0   2020-05-02   4.0                      4.0
1   2020-05-03   21.0                     17.0
2   2020-05-04   37.0                     16.0
3   2020-05-05   51.0                     14.0

CodePudding user response:

If indeed your DF has has the data in date order, then you might be able to get away with:

df['daily_cases'] = df['sum_cases'] - df['sum_cases'].shift(fill_value=0)
  • Related