Home > front end >  Get sum and percentage of a given dataframe
Get sum and percentage of a given dataframe

Time:07-15

Here is my dataframe for example

df = pd.DataFrame({'name': ['adam', 'adam', 'adam'], 
                   'meal': ['breakfast', 'lunch', 'dinner'], 
                   'cost': [75, 120, 250]})

   name       meal  cost
0  adam  breakfast    75
1  adam      lunch   120
2  adam     dinner   250

How can I get sum of all meals and insert to the last row and add a percentage column, as follows

   name       meal  cost  percent  
0  adam  breakfast    75    16.85
1  adam      lunch   120    26.97
2  adam     dinner   250    56.18
3  adam      total   445  

I tried to use cumsum() but it ended like this

df['total'] = df.groupby('name').cumsum()

   name       meal  cost  total
0  adam  breakfast    75     75
1  adam      lunch   120    195
2  adam     dinner   250    445

CodePudding user response:

Access the cost column and to get its sum

>>> df['cost'].sum()
445

Then divide the column values by the sum and multiply it by 100

>>> df['cost'] / df['cost'].sum() * 100

0    16.853933
1    26.966292
2    56.179775
Name: cost, dtype: float64

Assign the output to a column named percent

import pandas as pd

df = pd.DataFrame({'name': ['adam', 'adam', 'adam'], 
                   'meal': ['breakfast', 'lunch', 'dinner'], 
                   'cost': [75, 120, 250]})

df['percent'] = df['cost'] / df['cost'].sum() * 100

CodePudding user response:

You can compute percentage then add one row with value at the end of dataframe.

sum_cost = df['cost'].sum()
df['percent'] = df['cost'] / sum_cost * 100
df.loc[len(df)] = ['adam', 'total']   [sum_cost, df['percent'].sum()] 
print(df)

Output:

   name       meal  cost     percent
0  adam  breakfast    75   16.853933
1  adam      lunch   120   26.966292
2  adam     dinner   250   56.179775
3  adam      total   445  100.000000
  • Related