split_multi
split_multi(data=None, column=None, split='auto', prefix=None)Split a packed multi-select column into one column per answer.
Turns the single cell a spreadsheet export gives a “tick all that apply” question (“Speed; Drivers; Betting”) into the one-column-per-option layout the rest of the package expects, so calc_percentage_multi(), crosstab() and the plots all work on it.
The new columns are named prefix plus the answer text verbatim, which keeps the labels readable all the way through to a chart: calc_percentage_multi() strips the prefix back off and uses what remains as the option label. Surrounding whitespace is trimmed and empty answers are dropped, so “Speed; ; Drivers” yields two options. When no cell contains the delimiter, each cell is treated as a single answer. If you only want the percentages, skip this step: calc_percentage_multi() detects a packed column and splits it for you.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| data | pandas.DataFrame | A data frame. If omitted, the session default (use_dataset()) is used. |
None |
| column | str | The packed column. | None |
| split | str | The delimiter between answers inside a cell. “auto” (default) detects “;”, “|” or “,”, in that order. Pass a string to force one. | 'auto' |
| prefix | str | Prefix for the new column names. Defaults to the column’s own name followed by an underscore, e.g. motivations_. |
None |
Returns
| Name | Type | Description |
|---|---|---|
| pandas.DataFrame | The data with one new column per distinct answer, each holding the answer text for respondents who chose it and “” for those who did not, ordered most-chosen first. The packed column is kept. |
See Also
calc_percentage_multi, na_blank
Examples
>>> packed = pd.DataFrame({
... "respondent": [1, 2, 3, 4],
... "motivations": ["Speed; Drivers", "Speed", "", "Betting; Speed"],
... })
>>> split_multi(packed, "motivations")
>>> split_multi(packed, "motivations").pipe(
... calc_percentage_multi, "motivations_", id="respondent", sort="desc")