tally.dataset.DataSet.recode#

DataSet.recode(**kwargs)#

Create a new or copied series from data, recoded using a mapper.

This function takes a mapper of {key: logic} entries and injects the key into the target column where its paired logic is True. The logic may be arbitrarily complex and may refer to any other variable or variables in data. Where a pre-existing column has been used to start the recode, the injected values can replace or be appended to any data found there to begin with. The recoded data will always comply with the column type indicated for the target column according to the meta.

#### Mapping example:
recode_mapper = {

1: {“$union”:[{“$intersection”: [{‘locality’: [3]}, {“gender”:[1]}]},{“$intersection”: [{‘locality’: [4]}, {“gender”:[1]}]}] }, 2: {“$intersection”:[{‘locality’: [2]}, {‘gender’:[2]}]}, 3: {“$union”:[{‘locality’: [1]}, {‘gender’:[1]}]}, 4: {‘locality’: [4]}, 5: {‘locality’: [5]}, 6: {‘locality’: [6]}

}

Logical functions are strings preceded with the symbol $ and logic can be nested at an arbitrary depth.

Parameters
  • target (string) – The variable name of the target of the recode.

  • mapper (dict) – A mapper of {key: logic} entries.

  • default (string) – The column name to default to in cases where unattended lists are given in your logic, where an auto-transformation of {key: list} to {key: {default: list}} is provided. Note that lists in logical statements are themselves a form of shorthand and this will ultimately be interpreted as: {key: {default: has_any(list)}}.

  • append (boolean) – Should the new recoded data be appended to values already found in the series? If False, data from series (where found) will overwrite whatever was found for that item instead.

  • intersect (dict) – If a logical statement is given here then it will be used as an implied intersection of all logical conditions given in the mapper. For example, we could limit our mapper to males.

  • initialize (str (default: None)) – Name of variable to use to populate the variable before the recode

  • fillna (int) – If provided, can be used to fill empty/nan values.