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

Consolidate and show all the occurrences based on date and country

So I have a dataframe like that

Date        | Country | Occurrences
2021-08-25  |   CA    |     188
2021-08-25  |   AE    |     10
2021-08-25  |   BE    |     25
2021-08-25  |   GR    |     49
2021-08-26  |   CA    |     200
2021-08-26  |   AE    |     7
2021-08-26  |   BE    |     27
2021-08-25  |   GR    |     52

and I want to show it like this

Date        | CA  | AE | BE | GR
2021-08-25  | 188 | 10 | 25 | 49
2021-08-25  | 200 | 7  | 27 | 52

How do I go about doing it? I tried df.explode('Country') but it did not take into consideration the occurrences

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

>Solution :

Try using df.pivot_table().

data = {'Date': ['2021-08-25', '2021-08-25', '2021-08-25', '2021-08-25', '2021-08-26', '2021-08-26', '2021-08-26', '2021-08-25'],
        'Country': ['CA', 'AE', 'BE', 'GR', 'CA', 'AE', 'BE', 'GR'],
        'Occurrences': [188, 10, 25, 49, 200, 7, 27, 52]}
df = pd.DataFrame(data)
pivot_df = df.pivot_table(index='Date', columns='Country', values='Occurrences')
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