0

I have a data frame with patient_ID, date_of_admission and Hospital_name. And I would like to create a second data frame that counts the number of patients by date.

My data frame

data = {'Patient_admitted_id':  ['2323423', '234234234','234234234','324234234'],
        'Date': ['2021-01-01', '2021-01-01','2021-01-01', '2021-01-02'],
        'Hospital name' : ['Hospital1',"Hospital2","Hospital1", "Hospital3"]}

df = pd.DataFrame(data, columns=['Patient_admitted_id', 'Date', 'Hospital name' ])

Patient_admitted_id     Date      Hospital name
0   2323423           2021-01-01    Hospital1
1   234234234         2021-01-01    Hospital2
2   234234234         2021-01-01    Hospital1
3   324234234         2021-01-02    Hospital3
...

The data frame I am trying to create.

Date                 Cases        
0   2021-01-01        3             
1   2021-01-02        1             
2   2021-01-03        0             
...
  • Did you try with [groupby](https://stackoverflow.com/questions/47320572/pandas-groupby-and-count)? – anky May 08 '21 at 14:43

1 Answers1

1

Tested on different counts.

Option 1:

   date_count = df.groupby('Date').groups

enter image description here

Three are 3 cases in 2021-01-01 and 1 case in 2021-01-02 - as You wish.

Option 2:

cases = df.pivot_table(index = ['Date', 'Patient_admitted_id'], aggfunc ='size')

enter image description here

Priettify & Format

date_count = df.groupby('Date').count()

date_count

enter image description here

Piotr Żak
  • 2,046
  • 5
  • 18
  • 30