prepare_by_group_bar_graph

prepare_by_group_bar_graph(
    df,
    by_var,
    var,
    stat_fun=np.nanmean,
    *,
    order_by_stat=False,
    color='red',
)

Bar chart of a statistic of var computed within each by_var group.

Parameters

Name Type Description Default
df pd.DataFrame Data frame containing the grouping factor and the numeric variable. required
by_var str Grouping column. required
var str Numeric column to summarise. required
stat_fun Callable[[np.ndarray], float] Statistic applied to the non-missing values of each group. Defaults to :func:numpy.nanmean. Missing values are dropped before the call, matching R’s na.rm = TRUE. np.nanmean
order_by_stat bool If True, bars are ordered by the statistic (largest at the top); otherwise the groups keep their order of appearance. False
color str Bar fill color. 'red'

Returns

Name Type Description
ByGroupBarGraphResult df (columns by_var and stat_<var>) and the Plotly fig.

Examples

Basic — mean of a variable within each group:

import expdpy as ex
from expdpy.data import load_kuznets

df = load_kuznets()
ex.prepare_by_group_bar_graph(df, "continent", "gini_regional").fig

Advanced — a different statistic, bars ordered by it, a custom color, and the per-group values from .df:

import numpy as np

result = ex.prepare_by_group_bar_graph(
    df, "continent", "gini_regional",
    stat_fun=np.nanmedian, order_by_stat=True, color="#4682b4",
)
result.fig
result.df