[Solved] How to sum positive cases by date in python [closed]


you should aggregate by column and then sum the results, try this:

Notice that, the patient name should should have a numerical counter for keeping track.

import pandas as pd
import datetime
import numpy as np

# this a dummy set, you should have already this in your data frame
dict_df = {'Patient': [1,2,3,4,5], 'Positive': ['Positive'] * 5, 'Date': [datetime.date(2020, 4, 21), datetime.date(2020, 4, 22), datetime.date(2020, 4, 21), datetime.date(2020, 4, 23), datetime.date(2020, 4, 22)]}
df = pd.DataFrame(dict_df)

# create a numerics counter
cases = df['Positive'].to_numpy()
counter = np.where(cases == 'Positive', 1, 0)

# add column to data frame
df['counter'] = counter

# use groupby and sum
results = df['counter'].groupby(df['Date']).sum()
print(results)

#Results
#Date            Cases             
#2020-04-21        2
#2020-04-22        2
#2020-04-23        1

7

solved How to sum positive cases by date in python [closed]