Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert summary data (cumulative cases) to daily cases pandas

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    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

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading