recode_likert

recode_likert(x, levels=None, synonyms=None)

Recode a worded rating scale to integers.

Maps the worded answers of an ordinal rating question (e.g. “Very bad” … “Very good”) onto integers 1 to len(levels). Matching happens in three passes, in order: (1) an exact, case-insensitive, trimmed match against levels; (2) a substring fallback, so prefixed exports like “4 - Good” still resolve; (3) any synonyms you supply.

The substring passes try the longest wording first, so a scale whose levels nest inside one another (“Likeable” inside “Very likeable”, “Good” inside “Very good”) resolves to the level the answer actually names.

Parameters

Name Type Description Default
x list or Series Worded answers. required
levels list of str The scale’s answer wordings in ascending order. The first maps to 1, the last to len(levels). Defaults to a 5-point bad-to-good scale. None
synonyms dict Maps a canonical level (one of levels) to a string or list of alternative spellings that should map to the same integer. Matching is by case-insensitive substring. None

Returns

Name Type Description
pandas.Series Nullable integers (Int64) the same length as x; unmatched values are missing.

See Also

nps_group, ipm_model, calc_summary

Examples

>>> recode_likert(["Very bad", "Ok", "Good", "Very good"])
>>> recode_likert("4 - Good")
>>> recode_likert(["\U0001F642 Very good", "\U0001F610 Good"])
>>> recode_likert(["Dissatisfied", "Satisfied"],
...               synonyms={"Bad": "Dissatisfied", "Good": "Satisfied"},
...               levels=["Very bad", "Bad", "Ok", "Good", "Very good"])