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

Is it possible to summarize or group every row with a specific column value? – python

Picture of my dataframe

Is it possible to summarize or group every country’s info to something like a ‘total info’ row

This df is fluent, it will change each month and having a "quick access" view of how it looks will be very beneficial.

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

Take the picture as example: I would like to have Albania’s (every county’s) info in row so something like this

**ORIGINATING COUNTRY Calls Made Actual Qty Billable Qty. Cost (€)**

Albania 10 190 600 7

Zambia total total total

and total total total

every total total total

other total total total

country in my df total total total

I’ve tried groupby() and sum() but can figure it out.

>Solution :

import pandas as pd


df = pd.DataFrame(
    data=[
        ['Albania', 1, 10, 100, 0.1],
        ['Albania', 2, 20, 200, 0.2],
        ['Zambia', 3, 30, 300, 0.3],
        ['Zambia', 4, 40, 400, 0.4],
        [None, 5, 50, 500, 0.5],
        [None, 6, 60, 600, 0.6],
    ],
    columns=[
        'ORIGINATING COUNTRY',
        'Calls Made',
        'Actual Qty. (s)',
        'Billable Qty. (s)',
        'Cost (€)',
    ],
)

df['ORIGINATING COUNTRY'].replace({None: 'Unknown'}, inplace=True)

df.groupby('ORIGINATING COUNTRY', as_index=False).sum()

Output:

  ORIGINATING COUNTRY  Calls Made  Actual Qty. (s)  Billable Qty. (s)  Cost (€)
0             Albania           3               30                300       0.3
1             Unknown          11              110               1100       1.1
2              Zambia           7               70                700       0.7
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