weight_vector

weight_vector(data=None, weights=None)

Compute the per-respondent survey weights.

Returns the weights a scheme implies for a dataset (the same numbers the summary helpers use internally) so you can inspect them, attach them as a column, or feed them to another tool. Each respondent’s weight is target_share / sample_share for their category (raked across variables when there are several), normalised so the weighted base equals the unweighted one.

Parameters

Name Type Description Default
data pandas.DataFrame A data frame. If omitted, the session default is used. None
weights dict or list A weighting scheme (any form set_weights() accepts). If None (default), the session scheme is used. None

Returns

Name Type Description
pandas.Series One weight per row of data, with its index, normalised to mean 1.

See Also

set_weights, calc_percentage

Examples

>>> target = {"variable": "demo_gender",
...           "Male": 0.49, "Female": 0.50, "Non-binary": 0.01}
>>> w = weight_vector(podracing_survey, target)
>>> (podracing_survey.assign(weight=w)
...     .groupby("demo_gender")["weight"]
...     .agg(unweighted_pct="size", weighted_pct="sum") / len(podracing_survey) * 100)