0

I have a dataset that has a column for the actual age of the subject and another column that categorizes the age by 10s i.e. if the subject is 56, they are in their 50s. A sample of the table below:

age n_age
50s 56
60s 63
10s 17
0s 9
100s 105
20s 25
30s 33
80s 87
90s 92
70s 71
40s 48

The problem I have is the age column has a few null values and I am trying to fill them in based on the n_age in Python.

This is the code that I have tried to use

for i in enumerate(df['age']):
    if int(i) == range(0,9):
        df['age'] = df['age'].fillna('0s')
    if int(i) == range(10,19):
        df['age'] = df['age'].fillna('10s')
    if int(i) == range(20,29):
        df['age'] = df['age'].fillna('20s')
    if int(i) == range(30,39):
        df['age'] = df['age'].fillna('30s')
    if int(i) == range(40,49):
        df['age'] = df['age'].fillna('40s')
    if int(i) == range(50,59):
        df['age'] = df['age'].fillna('50s')
    if int(i) == range(60,69):
        df['age'] = df['age'].fillna('60s')
    if int(i) == range(70,79):
        df['age'] = df['age'].fillna('70s')
    if int(i) == range(80,89):
        df['age'] = df['age'].fillna('80s')
    if int(i) == range(90,99):
        df['age'] = df['age'].fillna('90s')
    if int(i) == range(100,109):
        df['age'] = df['age'].fillna('100s')

This didn't work and I don't know whats wrong with it or if I should do it another way.

Thanks in advance

  • If you are using a for loop with a pandas data frame, you are most likely doing something wrong. I suggest reading about "broadcasting" to get some ideas about how to solve your problem. – Code-Apprentice Dec 10 '21 at 23:18
  • 1
    Does this answer your question? [Pandas/Python: Set value of one column based on value in another column](https://stackoverflow.com/questions/49161120/pandas-python-set-value-of-one-column-based-on-value-in-another-column) – Code-Apprentice Dec 10 '21 at 23:23

0 Answers0