pythonpandasdataframeordinal

Separating pandas dataframe column data into new new columns


my data column combines information on two axes: wealth and life stage. I need to break up the two-digit codes by their 'tens'-place and 'ones'-place digits into two new ordinal variables. How would I go about separating and re-encoding this data?

This is the data dictionary for the column:

I have tried np.where() and created conditions:

I have used .map():

data2.map({11:'Wealthy', 12:'Wealthy', 13:'Wealthy', 14:'Wealthy', 15:'Wealthy',
#          21:'Prosperous', 22:'Prosperous', 23:'Prosperous', 24:'Prosperous', 25:'Prosperous',
#          31:'Comfortable', 32:'Comfortable', 33:'Comfortable', 34:'Comfortable', 35:'Comfortable',
#          41:'Less_Afflutent', 42:'Less_Afflutent', 43:'Less_Afflutent', 44:'Less_Afflutent', 45:'Less_Afflutent',
#          51:'Poorer', 52:'Poorer', 53:'Poorer', 54:'Poorer', 55:'Poorer'})

I've tried a couple other things as well that didn't seem worth even posting. I'm new and still learning but I've never come across a a problem like this before and I've done SO much googling


Solution

  • If I understand the question correctly, you want to split your current status column into two new columns and then make mappings for each of those.

    # Assuming your current column is called 'status'
    df['tens_place'] = df['status']//10 # whole number of division, no remainder, gives -1 for -1
    df['ones_place'] = df['status']%10 # just remainder, gives 9 for -1
    tens_map = tens_map = {1:'Wealthy Households',2:'Prosperous households',
        3:'Comfortable Households',4:'Less Affluent households' ,5:'Poorer households',
        -1:'unknown'}
    ones_map = ... # same sorta thing
    df['wealth_status'] = df['tens_place'].map(tens_map)
    df['family_status'] = df['ones_place'].map(ones_map)