calc_percentage_multi

calc_percentage_multi(
    data=None,
    prefix=None,
    id=None,
    by=None,
    sort='none',
    digits=0,
    drop=None,
    clean_names=True,
    split='auto',
)

Tabulate a check-all-that-apply (multi-select) question as percentages.

Multi-select questions arrive as a block of columns sharing a prefix, each holding the chosen option (or blank). This computes, per option, the share of respondents who selected it, so the percentages can (and usually do) sum to more than 100. The denominator is the number of distinct respondents who selected at least one option: if 700 of 1,000 respondents selected at least one motivation, each option’s percentage is “out of 700”. Supply id to count distinct respondents exactly; without it, each row counts as one respondent. With clean_names=True the option labels are stripped of prefix and exporter artefacts like “A…B” are turned back into “A / B”.

Two export shapes are handled. The usual one is a block of columns, one per option, named with a shared prefix. The other is a single column holding every answer a respondent ticked, joined by a delimiter, which is what Google Forms and most spreadsheet exports produce. If prefix matches exactly one column and a delimiter is found in it, that column is unpacked automatically and the answer text itself becomes the option label. Use split_multi() when you want those unpacked columns kept for other work.

Parameters

Name Type Description Default
data pandas.DataFrame A data frame. If omitted, the session default is used. None
prefix str Common column-name prefix identifying the option block, e.g. “motivations_”. None
id str Respondent identifier column used as the distinct-respondent denominator. If omitted, each row is treated as one respondent. None
by str or list of str Grouping column(s); percentages are computed within group. None
sort As in calc_percentage(). Sorting acts on the option labels. 'none'
digits As in calc_percentage(). Sorting acts on the option labels. 'none'
drop str or list of str Option labels to remove before counting (matched against the cleaned labels, case-insensitive). Defaults to the drop_answers option. None
clean_names bool If True (default), tidy the option labels by stripping prefix and un-mangling exporter artefacts (“A…B” to “A / B”). True
split str or bool How to handle a question packed into a single cell per respondent (“Speed; Drivers”). “auto” (default) detects “;”, “|” or “,” and splits on it; pass a string to force a delimiter, or False never to split. Only ever applies when prefix selects exactly one column. 'auto'

Returns

Name Type Description
pandas.DataFrame option, n (respondents choosing it) and pct.

See Also

calc_percentage, split_multi, plot_bars

Examples

>>> calc_percentage_multi(podracing_survey, "motivations_", id="respondent_id", sort="desc")
>>> packed = pd.DataFrame({
...     "respondent": [1, 2, 3, 4],
...     "motivations": ["Speed; Drivers", "Speed", "", "Betting; Speed"],
... })
>>> calc_percentage_multi(packed, "motivations", id="respondent", sort="desc")