Home > Blockchain >  Is it possible to summarize or group every row with a specific column value? - python
Is it possible to summarize or group every row with a specific column value? - python

Time:11-15

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.

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.

CodePudding user response:

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').sum()

Output:

                     Calls Made  Actual Qty. (s)  Billable Qty. (s)  Cost (€)
ORIGINATING COUNTRY                                                          
Albania                       3               30                300       0.3
Unknown                      11              110               1100       1.1
Zambia                        7               70                700       0.7
  • Related