calc_percentage

calc_percentage(
    data=None,
    column=None,
    by=None,
    sort='none',
    levels=None,
    digits=0,
    wide=False,
    na_rm=True,
    drop=None,
    weights=None,
)

Count a categorical question as percentages.

The headline helper: one call that returns both the raw counts and the percentages, optionally grouped by one or more variables and optionally pivoted to a wide cross-tabulation.

Percentages are computed within each group, so they sum to about 100 per group (subject to rounding). The order of column is decided in this order of precedence: an explicit levels argument; then a non-“none” sort; then a registered order for the variable (see register_order()); then the column’s own categories, when it is categorical; otherwise data order. When by is omitted, the default_by option is used if set, so you can apply a standard breakdown without repeating it. wide=True pivots to one row per group with a column per answer: the shape you want for a slide table or an Excel tab.

Parameters

Name Type Description Default
data pandas.DataFrame A data frame. If omitted, the session default set with use_dataset() is used. None
column str The categorical column to tabulate. None
by str or list of str Grouping column(s). Percentages are computed within each group so they sum to about 100 per group. None
sort str Order of column: “none” (default, data order), “desc” (largest percentage first) or “asc”. 'none'
levels list of str An explicit level order for column. Overrides sort. If omitted and sort="none", a registered order for this variable is applied automatically, and failing that a categorical keeps its own category order. None
digits int Decimal places for the percentage. Default 0. 0
wide bool If True, pivot to one row per by group and one column per answer (dropping n). Default False (tidy long form). False
na_rm bool If True (default), blanks and “Prefer not to answer” responses (see na_blank()) are dropped before counting. True
drop str or list of str Answer values to remove before counting (e.g. ["Other", "Don't know"]), so the kept answers re-base to about 100%. Matching is case-insensitive (see drop_items()). Defaults to the drop_answers option (None = drop nothing). None
weights (bool, dict or list) Survey weighting for this call: None (default) uses the session scheme from set_weights() if one is set; False forces unweighted; or pass an ad-hoc scheme (any form set_weights() accepts). When weighting is active a wpct column (weighted percentage) is added beside n and pct. None

Returns

Name Type Description
pandas.DataFrame column, n and pct (and wpct when weighting is active) in long form, or one row per group with an answer column each (wide form).

See Also

calc_percentage_multi, calc_percentage_batch, calc_summary, register_order

Examples

>>> calc_percentage(podracing_survey, "demo_gender")
>>> calc_percentage(podracing_survey, "demo_gender", sort="desc")
>>> calc_percentage(podracing_survey, "satis_return", by="region", wide=True)