Skip to content
Advertisement

Recode multiple values in several columns in Python [similar to R]

I am trying to translate my R script to python. I have a survey data with several date of birth and education level columns for each family member(from family member 1 to member 10): here a sample:

JavaScript

I had a function in R in order to check the logic and re code wrong education level in all columns.Like this

JavaScript

and apply it for all columns like this:

JavaScript

is there a way to do similar process in Python instead of manually recoding each column?

Advertisement

Answer

You can write a function that combines pipe with np.select, as well as a dictionary (to abstract as much manual processing as possible):

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement