Banner tables

import ezrsurvey as ez

The banner is the table a client actually works from: every question down the side, every way of splitting the sample across the top, and one column for the sample as a whole. It is the first deliverable of most projects and the one that takes longest to build by hand, because it is the same cross-tab written out dozens of times.

crosstab_banner() writes it in one call.

The shape of it

Name the questions as rows and the splits as cols.

ez.crosstab_banner(
    ez.podracing_survey,
    rows=["satis_return", "demo_edu"],
    cols=["demo_gender", "region"],
)
variable item Overall Female Male Non-binary Asia Europe Latin America North America Oceania
0 demo_edu Bachelor or equivalent 25.0 26.0 25.0 22.0 23.0 28.0 15.0 25.0 22.0
1 demo_edu Doctoral or equivalent 4.0 3.0 4.0 4.0 4.0 3.0 5.0 3.0 7.0
2 demo_edu Lower secondary 11.0 12.0 11.0 15.0 12.0 10.0 10.0 13.0 5.0
3 demo_edu Master or equivalent 13.0 12.0 13.0 19.0 11.0 15.0 9.0 12.0 15.0
4 demo_edu Primary or less 4.0 3.0 5.0 7.0 7.0 6.0 4.0 3.0 2.0
5 demo_edu Short-cycle tertiary 13.0 14.0 12.0 4.0 19.0 11.0 17.0 12.0 10.0
6 demo_edu Upper secondary 30.0 30.0 31.0 30.0 25.0 26.0 41.0 31.0 39.0
7 satis_return Likely 27.0 28.0 27.0 15.0 24.0 28.0 30.0 26.0 31.0
8 satis_return Not sure 24.0 25.0 22.0 33.0 25.0 25.0 21.0 24.0 12.0
9 satis_return Unlikely 16.0 14.0 17.0 26.0 12.0 16.0 11.0 17.0 24.0
10 satis_return Very likely 26.0 24.0 26.0 26.0 34.0 25.0 28.0 24.0 24.0
11 satis_return Very unlikely 8.0 9.0 7.0 0.0 5.0 6.0 10.0 8.0 10.0

Three things are worth noticing in that output. Every question keeps its own answers as rows, so questions with different scales stack in one table. The Overall column is the whole sample, which is what a reader compares each group against. And the percentages are column percentages: they run down each group, so they answer “what does this group look like”, not “where do this answer’s respondents come from”.

Selectors work, which is how a block of rating questions goes in:

ez.crosstab_banner(
    ez.podracing_survey,
    rows=ez.starts_with("ratings_"),
    cols="demo_gender",
)
variable item Overall Female Male Non-binary
0 ratings_atmosphere Bad 14.0 13.0 15.0 7.0
1 ratings_atmosphere Good 30.0 29.0 32.0 15.0
2 ratings_atmosphere Ok 25.0 28.0 21.0 48.0
3 ratings_atmosphere Very bad 5.0 5.0 6.0 0.0
4 ratings_atmosphere Very good 26.0 25.0 26.0 30.0
5 ratings_commentary Bad 33.0 33.0 33.0 33.0
6 ratings_commentary Good 15.0 15.0 16.0 11.0
7 ratings_commentary Ok 32.0 33.0 32.0 37.0
8 ratings_commentary Very bad 16.0 15.0 17.0 15.0
9 ratings_commentary Very good 4.0 4.0 3.0 4.0
10 ratings_safety Bad 24.0 24.0 24.0 26.0
11 ratings_safety Good 21.0 18.0 23.0 37.0
12 ratings_safety Ok 35.0 40.0 33.0 11.0
13 ratings_safety Very bad 12.0 10.0 13.0 26.0
14 ratings_safety Very good 8.0 8.0 7.0 0.0
15 ratings_speed Bad 16.0 16.0 16.0 11.0
16 ratings_speed Good 30.0 29.0 31.0 26.0
17 ratings_speed Ok 27.0 28.0 26.0 30.0
18 ratings_speed Very bad 5.0 6.0 4.0 7.0
19 ratings_speed Very good 22.0 22.0 22.0 26.0
20 ratings_value Bad 24.0 24.0 23.0 19.0
21 ratings_value Good 19.0 19.0 18.0 22.0
22 ratings_value Ok 28.0 27.0 30.0 37.0
23 ratings_value Very bad 17.0 18.0 16.0 15.0
24 ratings_value Very good 12.0 11.0 13.0 7.0
25 ratings_venue Bad 23.0 22.0 24.0 30.0
26 ratings_venue Good 27.0 27.0 28.0 30.0
27 ratings_venue Ok 30.0 29.0 29.0 33.0
28 ratings_venue Very bad 9.0 11.0 8.0 4.0
29 ratings_venue Very good 11.0 12.0 11.0 4.0

Numbers and categories in one table

A numeric question has no answers to count, so counting it would produce a row per distinct value. Handed one, the banner switches to a statistics block instead, and says so by labelling the rows:

ez.crosstab_banner(
    ez.podracing_survey,
    rows=["nps_value", "demo_age"],
    cols="demo_gender",
)
variable item Overall Female Male Non-binary
0 demo_age mean 32.59 32.88 32.30 31.19
1 demo_age median 32.00 33.00 32.00 31.00
2 demo_age sd 11.18 11.41 11.07 11.14
3 demo_age p25 24.00 25.00 24.00 23.50
4 demo_age p75 40.00 40.00 40.00 37.00
5 nps_value mean 7.56 7.51 7.61 7.56
6 nps_value median 8.00 8.00 8.00 8.00
7 nps_value sd 1.76 1.81 1.72 1.69
8 nps_value p25 6.00 6.00 7.00 7.00
9 nps_value p75 9.00 9.00 9.00 8.00

cell overrides the choice when you want something specific: "pct", "count", "mean", or "diff" for each group’s distance from Overall, which is the version to read when you are hunting for what makes a group different.

ez.crosstab_banner(
    ez.podracing_survey,
    rows="satis_return",
    cols="demo_gender",
    cell="diff",
)
variable item Overall Female Male Non-binary
0 satis_return Likely 27.0 1.0 0.0 -12.0
1 satis_return Not sure 24.0 1.0 -2.0 9.0
2 satis_return Unlikely 16.0 -2.0 1.0 10.0
3 satis_return Very likely 26.0 -2.0 0.0 0.0
4 satis_return Very unlikely 8.0 1.0 -1.0 -8.0

Weighting

A banner built on a weighted sample has to be weighted in every cell, not corrected afterwards. Set the scheme once and it applies throughout:

ez.set_weights({
    "variable": "demo_gender",
    "Male": 0.49,
    "Female": 0.50,
    "Non-binary": 0.01,
})

weighted = ez.crosstab_banner(ez.podracing_survey, rows="satis_return", cols="region")

ez.clear_weights()
weighted
Weighting scheme stored for: demo_gender. It applies once a dataset is in play.
variable item Overall Asia Europe Latin America North America Oceania
0 satis_return Likely 27.0 23.0 28.0 29.0 27.0 31.0
1 satis_return Not sure 24.0 26.0 25.0 23.0 24.0 13.0
2 satis_return Unlikely 16.0 12.0 15.0 11.0 17.0 25.0
3 satis_return Very likely 25.0 33.0 25.0 28.0 24.0 21.0
4 satis_return Very unlikely 8.0 6.0 7.0 9.0 8.0 10.0

Weights are computed once per dataset and scheme and then reused, rather than re-running the raking loop for every cell. clear_weights_cache() empties that store if you need to force a recalculation.

Letting it choose the variables

Called with no rows or cols, the banner picks them itself: every question that looks like a question, crossed by every variable that looks like a split. That choice decides the entire table, so in an interactive session it shows the choice and waits:

Banner variables chosen automatically: 34 question(s) across 12 grouping variable(s).
  Questions (34): satis_return, demo_gender, ...
  Grouping variables (12): demo_gender, region, ...
  Skipped (11): respondent_id, start_date, nps_com, ...
  Name `rows` / `cols` yourself, or raise `max_levels` (now 20), to change this.
Continue? [Y/n]

Identifier columns, free text and anything with more distinct answers than max_levels are left out. Naming the variables yourself is your own decision and is never questioned. The prompt only appears when someone is there to answer it, so an unattended script cannot stall on it; ez.ezrsurvey_options(confirm=False) removes it and True forces it.

Runs long enough to worry about

A full every-variable banner on a wide survey is minutes of work. While it runs it says where it is, with an estimate built from the throughput measured so far rather than a guess:

Banner table: 34 question(s) across 12 grouping variable(s)
[7/34] satis_return  (about 2m left)

Progress reporting is on in an interactive session and silent in scripts and tests. ez.ezrsurvey_options(progress=False) turns it off everywhere.

For a table long enough that losing it would hurt, ask for a checkpoint:

ez.crosstab_banner(survey, checkpoint=True)           # managed for you
ez.crosstab_banner(survey, checkpoint="banner.pkl")   # or choose the location

Each question is saved as it finishes, so re-running the identical call after a crash carries on from where it stopped. The managed file is named after the run’s own fingerprint, so a call finds its own interrupted run and never sees one belonging to different data or different arguments: a stale file cannot quietly corrupt a table. A finished run deletes the managed file; a file you named is yours and stays where you put it. clear_checkpoints() clears anything left in the managed folder by a run that was never repeated.

Getting it out

A banner is usually delivered as a spreadsheet, and long=True gives the tidy form to pivot or join instead of the printed one.

banner = ez.crosstab_banner(survey, rows=..., cols=...)

ez.export_xlsx(banner, sheet_names=["Banner"], path="banner.xlsx")
ez.save_data(banner, "banner.csv")

A data frame has a single header row, so the banner carries its second one, the name of each grouping variable spanning its answers, in banner.attrs["banner_spanners"]. flextable=True returns the table with a real two-level header instead, which is what to hand to report_add_table() for a slide or a Word document:

ez.crosstab_banner(
    ez.podracing_survey,
    rows="satis_return",
    cols=["demo_gender", "region"],
    flextable=True,
).head()
Variable Item Overall demo_gender region
Variable Item Overall Female Male Non-binary Asia Europe Latin America North America Oceania
0 satis_return Likely 27.0 28.0 27.0 15.0 24.0 28.0 30.0 26.0 31.0
1 satis_return Not sure 24.0 25.0 22.0 33.0 25.0 25.0 21.0 24.0 12.0
2 satis_return Unlikely 16.0 14.0 17.0 26.0 12.0 16.0 11.0 17.0 24.0
3 satis_return Very likely 26.0 24.0 26.0 26.0 34.0 25.0 28.0 24.0 24.0
4 satis_return Very unlikely 8.0 9.0 7.0 0.0 5.0 6.0 10.0 8.0 10.0