crosstab
crosstab
Cross-tabulate two survey questions as counts, percentages or an aggregate of a third column. The banner table builds its blocks from these crosstabs.
Functions
| Name | Description |
|---|---|
| apply_margin_orders | Put both margins in their order, levels and rows alike, where they have one. |
| crosstab | Cross-tabulate two survey questions. |
| crosstab_long | The crosstab cells in long form, under the fixed names .x, .y and .value. |
| mean_or_missing | The mean, as R’s mean(): missing when any value is missing. |
| order_rank | Each value’s position in levels, or in its own order of appearance when there are none. |
| value_cells | Each x-by-y cell’s aggregate of the value column: fn, or the weighted mean when weighted. |
apply_margin_orders
crosstab.apply_margin_orders(out, x_levels, y_levels)Put both margins in their order, levels and rows alike, where they have one.
crosstab
crosstab.crosstab(
data=None,
x=None,
y=None,
cell='count',
value=None,
fn=None,
wide=True,
digits=None,
na_rm=True,
drop=None,
weights=None,
)Cross-tabulate two survey questions.
Builds a crosstab from two categorical columns: x forms the rows and y the columns. Choose what the cells mean with cell: “row_pct” makes each row sum to 100 (the distribution of y within each x), “col_pct” makes each column sum to 100, “total_pct” is the share of the whole table, and “count” is the raw frequency. Supplying a numeric value switches the cells to an aggregate of that column (by default the mean), which answers questions like “what is the average NPS for each region by gender?”. Blanks and non-answers in x/y are dropped when na_rm=True. Any registered orders (register_order()) set the row and column ordering automatically; a categorical without one keeps its own category order; anything else stays in data order.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| data | pandas.DataFrame | A data frame. If omitted, the session default is used. | None |
| x | str | Row variable. | None |
| y | str | Column variable. | None |
| cell | str | What to put in each cell when value is not given: “count” (default), “row_pct”, “col_pct” or “total_pct”. |
'count' |
| value | str | Numeric column to aggregate per cell instead of counting. | None |
| fn | callable | Aggregation function used with value, given each cell’s numbers as a Series. Default: the mean. |
None |
| wide | bool | If True (default), return one row per x with a column per y level; if False, return the tidy long form. |
True |
| digits | int | Decimal places for percentage / aggregated cells. Default 0 for percentages, 2 when value is supplied. |
None |
| na_rm | bool | Drop blanks / non-answers in x, y (and missing value). Default True. |
True |
| drop | str or list of str | Answer values to remove from both x and y before tabulating (e.g. ["Other"]), matched case-insensitively. Defaults to the drop_answers option. 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. When weighting is active the cells are weighted: weighted counts, weighted percentages, or (for a numeric value) the weighted mean. |
None |
Returns
| Name | Type | Description |
|---|---|---|
| pandas.DataFrame | Wide (x plus one column per y level) or long (x, y, value). The long form refuses an x or y that is itself called value, since the two columns would share a name. |
See Also
calc_percentage, compare_values, register_order
Examples
>>> crosstab(podracing_survey, "demo_gender", "region")
>>> crosstab(podracing_survey, "region", "demo_gender", cell="row_pct")
>>> crosstab(podracing_survey, "region", "demo_gender", value="nps_value")crosstab_long
crosstab.crosstab_long(data, x_name, y_name, request)The crosstab cells in long form, under the fixed names .x, .y and .value.
The counting runs on a frame built here from only the columns it needs, so a survey column called value or n cannot collide with the names the counting uses. The banner table reads this directly for the same reason.
Args: data: The survey DataFrame. x_name: The row question. y_name: The column question. request: A dict of cell, value (a column name or None), fn, digits, na_rm, drop and weights.
Returns: A DataFrame of .x, .y and .value, one row per cell, with categorical margins wherever an order applies.
mean_or_missing
crosstab.mean_or_missing(values)The mean, as R’s mean(): missing when any value is missing.
order_rank
crosstab.order_rank(values, levels=None)Each value’s position in levels, or in its own order of appearance when there are none.
value_cells
crosstab.value_cells(frame, weighted, request)Each x-by-y cell’s aggregate of the value column: fn, or the weighted mean when weighted.