piper.verbs.adorn_totals

piper.verbs.adorn_totals(df: pandas.core.frame.DataFrame, columns: Optional[Union[str, list]] = None, fillna: Union[str, int] = '', name: str = 'All', axis: Union[int, str] = 0, ignore_index: bool = False)pandas.core.frame.DataFrame[source]

add totals to a dataframe

Based on R janitor package function add row and/or column totals to a dataframe.

Examples

df = sample_matrix(seed=42)
df = adorn_totals(df, ['a', 'c'], axis='row')
head(df, 10, tablefmt='plain')

       a       b    c       d        e
0     15   8.617   16  25.23     7.658
1      8  25.792   18   5.305   15.426
2      5   5.343   12  -9.133   -7.249
3      4  -0.128   13   0.92    -4.123
4     25   7.742   11  -4.247    4.556
All   57           70
url = 'https://github.com/datagy/pivot_table_pandas/raw/master/sample_pivot.xlsx'
df = pd.read_excel(url, parse_dates=['Date'])
head(df)

Date        Region                Type     Units        Sales
2020-07-11  East    Children's Clothing         18.0      306
2020-09-23  North   Children's Clothing         14.0      448

g1 = df.groupby(['Type', 'Region']).agg(TotalSales=('Sales', 'sum')).unstack()
g1 = adorn_totals(g1, axis='both').astype(int)
g1 = flatten_names(g1, remove_prefix='TotalSales')
g1

                          East     North     South     West      All
  Children's Clothing    45849     37306     18570    20182   121907
  Men's Clothing         51685     39975     18542    19077   129279
  Women's Clothing       70229     61419     22203    22217   176068
  All                   167763    138700     59315    61476   427254
Parameters
  • df – Pandas dataframe

  • columns – columns to be considered on the totals row. Default None - All columns considered.

  • fillna – fill NaN values (default is ‘’)

  • name – name of row/column title (default ‘Total’)

  • axis – axis to apply total (values: 0 or ‘row’, 1 or ‘column’) To apply totals to both axes - use ‘both’. (default is 0)

  • ignore_index – default False. When concatenating totals, ignore index in both dataframes.

Returns

Return type

A pandas DataFrame with additional totals row and/or column total.