calc_percentage_batch

calc_percentage_batch(
    data=None,
    *columns,
    by=None,
    sort='none',
    digits=0,
    na_rm=True,
    drop=None,
    weights=None,
    clean_names=False,
    prefix=None,
)

Percentages for a batch of questions at once.

Runs calc_percentage() over several columns and stacks the results into one tidy table with a variable column identifying the source question and a shared answer column: handy for tabulating a whole block of questions (e.g. every demo_ variable) in a single call. Answers are stacked as text, since level orders differ between questions. To send each question to its own Excel tab instead, see export_xlsx().

variable is the raw column name by default. Set clean_names=True (or name a prefix) to get the same tidied wording calc_percentage_multi() and ipm_model() produce, which is what lets a batch table join to a model table on the question name.

Parameters

Name Type Description Default
data pandas.DataFrame A data frame. If omitted, the session default is used. None
*columns str or selector Columns to tabulate: names, or selectors such as starts_with("demo_"). ()
by Passed to calc_percentage() (so a wpct column appears when weighting is active, and drop removes unwanted answers from every question). None
sort Passed to calc_percentage() (so a wpct column appears when weighting is active, and drop removes unwanted answers from every question). None
digits Passed to calc_percentage() (so a wpct column appears when weighting is active, and drop removes unwanted answers from every question). None
na_rm Passed to calc_percentage() (so a wpct column appears when weighting is active, and drop removes unwanted answers from every question). None
drop Passed to calc_percentage() (so a wpct column appears when weighting is active, and drop removes unwanted answers from every question). None
weights Passed to calc_percentage() (so a wpct column appears when weighting is active, and drop removes unwanted answers from every question). None
clean_names bool If True, tidy each variable with clean_label() instead of reporting the raw column name. False
prefix str Question prefix to strip from variable, e.g. “ratings_”. Implies clean_names. None

Returns

Name Type Description
pandas.DataFrame variable, answer, n, pct (plus any by columns).

See Also

calc_percentage, clean_label, export_xlsx

Examples

>>> calc_percentage_batch(podracing_survey, "demo_gender", "demo_job")
>>> calc_percentage_batch(podracing_survey, starts_with("demo_"))
>>> calc_percentage_batch(podracing_survey, starts_with("ratings_"), prefix="ratings_")