crosstab_banner

crosstab_banner

Build the market-research banner table: questions down the side, groups across the top, an Overall column first. Long runs report progress, confirm automatic selections and can resume from a checkpoint.

Functions

Name Description
banner_assemble_wide Spread the long banner into the master table, recording each column’s group in attrs.
banner_auto_select The default questions and groups when the caller names none, and what was skipped.
banner_block_cat One categorical question as column percentages (or counts) within Overall and each group.
banner_block_multi One check-all-that-apply question: each option’s share of the respondents who ticked any.
banner_block_num One numeric question as a statistics block for Overall and within each group.
banner_checkpoint_load Pick up an interrupted run; a checkpoint from a different run is discarded, never blended in.
banner_checkpoint_save Record what is done so far, through a neighbouring temporary file so a crash cannot half-write it.
banner_fingerprint What a saved run must match before any of it is reused: the data and every shaping argument.
banner_numeric_stats The chosen statistics of a numeric column, weighted when weights is given, in which order.
banner_output Shape the finished blocks into the long form, the wide table, or the two-row-header table.
banner_table The wide banner with a two-row header: each grouping variable spanning its items.
block_members Every column that belongs to one of the multi-select blocks.
build_block One question’s block, by its kind: multi-select, numeric statistics, or categorical percentages.
clear_checkpoints Delete saved cross-tab checkpoints.
confirm_banner Show an automatic selection and ask before the run; a named selection is never questioned.
crosstab_banner Build a master banner (cross-tab) table.
fill_grid Every item crossed with every group item, taking seen values and filling the rest with 0.
open_checkpoint The checkpoint this run writes to, and whatever an interrupted identical run left in it.
run_blocks Build each question’s block in turn, announcing it first and checkpointing after it.
stub_specs The stub questions in column order: single variables and multi-select blocks.

banner_assemble_wide

crosstab_banner.banner_assemble_wide(
    long,
    row_keys,
    col_vars,
    group_levels,
    total,
)

Spread the long banner into the master table, recording each column’s group in attrs.

banner_auto_select

crosstab_banner.banner_auto_select(data, max_levels)

The default questions and groups when the caller names none, and what was skipped.

banner_block_cat

crosstab_banner.banner_block_cat(d_all, variable, settings)

One categorical question as column percentages (or counts) within Overall and each group.

banner_block_multi

crosstab_banner.banner_block_multi(data, prefix, settings)

One check-all-that-apply question: each option’s share of the respondents who ticked any.

banner_block_num

crosstab_banner.banner_block_num(data, variable, settings)

One numeric question as a statistics block for Overall and within each group.

banner_checkpoint_load

crosstab_banner.banner_checkpoint_load(path, fingerprint)

Pick up an interrupted run; a checkpoint from a different run is discarded, never blended in.

banner_checkpoint_save

crosstab_banner.banner_checkpoint_save(
    path,
    fingerprint,
    finished,
    blocks,
    row_keys,
)

Record what is done so far, through a neighbouring temporary file so a crash cannot half-write it.

banner_fingerprint

crosstab_banner.banner_fingerprint(data, labels, settings)

What a saved run must match before any of it is reused: the data and every shaping argument.

banner_numeric_stats

crosstab_banner.banner_numeric_stats(x, weights, which, digits)

The chosen statistics of a numeric column, weighted when weights is given, in which order.

banner_output

crosstab_banner.banner_output(state, settings, long, flextable)

Shape the finished blocks into the long form, the wide table, or the two-row-header table.

banner_table

crosstab_banner.banner_table(wide)

The wide banner with a two-row header: each grouping variable spanning its items.

block_members

crosstab_banner.block_members(multi)

Every column that belongs to one of the multi-select blocks.

build_block

crosstab_banner.build_block(data, d_all, spec, settings)

One question’s block, by its kind: multi-select, numeric statistics, or categorical percentages.

clear_checkpoints

crosstab_banner.clear_checkpoints()

Delete saved cross-tab checkpoints.

Removes the checkpoint files that crosstab_banner(checkpoint=True) keeps in the ezrsurvey cache folder (the folder R’s tools::R_user_dir() names). A finished run clears its own checkpoint, so anything left is from a run that was interrupted and never repeated; this empties the folder in one call. A checkpoint you pointed somewhere yourself (checkpoint="my-run.pkl") is your file and is never touched.

Returns

Name Type Description
int The number of files removed.

See Also

crosstab_banner, clear_weights_cache

Examples

>>> clear_checkpoints()

confirm_banner

crosstab_banner.confirm_banner(chosen, specs, max_levels, confirm)

Show an automatic selection and ask before the run; a named selection is never questioned.

crosstab_banner

crosstab_banner.crosstab_banner(
    data=None,
    rows=None,
    cols=None,
    cell='auto',
    stats=None,
    total=True,
    digits=None,
    na_rm=True,
    drop=None,
    weights=None,
    max_levels=MAX_LEVELS,
    long=False,
    flextable=False,
    checkpoint=None,
    confirm=None,
)

Build a master banner (cross-tab) table.

Produces the market-research “banner” table: one master table with a stack of question variables down the side (rows) and one or more grouping variables across the top (cols), plus an Overall column for the whole sample. Categorical questions become column-percentage blocks (each banner column sums to about 100 within the block); numeric questions become a mean / median / sd / quartile block. It generalises crosstab() from a single pair to a whole table, and picks the cell content per question automatically.

A DataFrame has a single header row, so the two-row banner header (each grouping variable’s name spanning its items) is carried in .attrs["banner_spanners"], and flextable=True returns the table with a real two-level header. Registered orders (register_order()) set the item and banner-column ordering automatically. Supplying only the data frame builds the full every-question-by-every-question matrix: every variable with at most max_levels distinct answers becomes both a stub and a banner group, numeric scales are added as stub statistics blocks, and identifier / free-text columns are skipped (and named in a message). That selection decides the whole table, so an interactive session prints it and waits for a yes before starting the run (see confirm). Check-all-that-apply blocks are recognised as one multi-select stub question and tabulated with calc_percentage_multi(), whose base is the respondents who picked any option (so those rows can sum past 100); they appear as stubs only and are unweighted.

A full every-variable banner is one crosstab() per question per grouping variable, so a wide survey takes a while. In an interactive session it reports which question it is on, with an estimate of the time left; ezrsurvey_options(progress=False) turns that off, and scripts are silent by default. checkpoint=True saves each question as it completes, so re-running the identical call after an interruption resumes instead of starting again; that file is deleted as soon as the table is built. Pass a path instead to choose the location yourself; that file is yours, and is never deleted. clear_checkpoints() empties the managed folder.

Parameters

Name Type Description Default
data pandas.DataFrame A data frame. If omitted, the session default is used. None
rows (str, selector or list) Stub variables, down the side (e.g. ["satis_return", "demo_edu"] or starts_with("ratings_")). If omitted, every eligible variable is used. None
cols (str, selector or list) Banner / grouping variables, across the top. Pass several to get several spanning column groups. If omitted, every eligible variable is used. None
cell str What each body cell holds: “auto” (default) uses column percentages for categorical questions and a statistics block for numeric ones; “pct” forces column percentages; “count” uses (weighted) counts; “mean” forces a numeric mean (errors on a categorical question); “diff” shows each cell’s difference from the Overall column. 'auto'
stats list of str For numeric questions, which statistics form the block: any of “mean”, “median”, “sd”, “p25”, “p75”. Default all five. None
total bool Include the whole-sample Overall column. Default True. True
digits int Decimal places. None (default) uses 0 for percentages / counts and 2 for numeric statistics; a value overrides both. None
na_rm bool Drop blanks / non-answers in the questions and banner variables. Default True. True
drop str or list of str Answer values to remove before tabulating (see drop_items()). None
weights (bool, dict or list) Survey weighting: None (default) uses the session scheme from set_weights() if set; False forces unweighted; or pass an ad-hoc scheme. None
max_levels int When rows / cols are omitted, the largest number of distinct answers a categorical variable may have to be used automatically (default 20). MAX_LEVELS
long bool If True, return the tidy long form (variable, item, group, group_item, value). Default False. False
flextable bool If True, return the table with a two-level column header (each grouping variable spanning its items), pandas’ form of R’s flextable. Default False. False
checkpoint bool or str Save each question as it finishes, so re-running the same call picks up where an interrupted run stopped. True keeps the file in the ezrsurvey cache folder and deletes it once the run completes; a file path puts it where you choose and leaves it there. None (default) or False writes nothing. None
confirm bool When rows / cols are left to the automatic selection, show which variables were chosen and which were skipped, and wait for a yes before the run starts. None (default) follows the confirm option, which asks in an interactive session and never asks in a script. Answering no returns None and computes nothing. None

Returns

Name Type Description
pandas.DataFrame or None By default the wide table: variable, item, Overall, then one column per banner item, with .attrs["banner_spanners"] naming each column’s group. With long=True, the tidy long form.

See Also

crosstab, calc_percentage_batch, export_summary_xlsx, register_order

Examples

>>> crosstab_banner(podracing_survey,
...                 rows=["satis_return", "demo_edu"],
...                 cols=["demo_gender", "region"])
>>> crosstab_banner(podracing_survey, rows=["nps_value", "demo_age"], cols="demo_gender")
>>> crosstab_banner(podracing_survey, rows="satis_return", cols="region", cell="diff")
>>> crosstab_banner(podracing_survey)

fill_grid

crosstab_banner.fill_grid(variable, group, items, group_items, seen)

Every item crossed with every group item, taking seen values and filling the rest with 0.

open_checkpoint

crosstab_banner.open_checkpoint(data, specs, settings, checkpoint)

The checkpoint this run writes to, and whatever an interrupted identical run left in it.

run_blocks

crosstab_banner.run_blocks(data, specs, settings, saved)

Build each question’s block in turn, announcing it first and checkpointing after it.

Args: data: The survey data. specs: The stub questions, from stub_specs(). settings: The run’s options. saved: The checkpoint path, fingerprint and what an earlier run finished.

Returns: The finished question labels, their long blocks and their row keys.

stub_specs

crosstab_banner.stub_specs(data, row_singles, multi)

The stub questions in column order: single variables and multi-select blocks.