Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

fill missing value based on one column to another

I have two columns like this:

enter image description here

what i want to do is suppose for ‘age’ columns value between 30-39,i want to fill the missing value of age_band = 30. Like that suppose for ‘age’ columns value between 80-89,i want to fill the missing value of age_band = 80. How can i do this in pandas dataframe?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried like this but the loop is running like forever

for ages in data['age']:
if 0<=ages<=9:
    data['age_band']= data['age_band'].fillna(0)
elif 10<=ages<=19:
    data['age_band']= data['age_band'].fillna(10)
elif 20<=ages<=29:
    data['age_band']= data['age_band'].fillna(20)
elif 30<=ages<=39:
    data['age_band']= data['age_band'].fillna(30)
elif 40<=ages<=49:
    data['age_band']= data['age_band'].fillna(40)
elif 50<=ages<=59:
    data['age_band']= data['age_band'].fillna(50)
elif 60<=ages<=69:
    data['age_band']= data['age_band'].fillna(60)
elif 70<=ages<=79:
    data['age_band']= data['age_band'].fillna(70)
elif 80<=ages<=89:
    data['age_band']= data['age_band'].fillna(80)
elif 90<=ages<=99:
    data['age_band']= data['age_band'].fillna(90)
elif 100<=ages<=109:
    data['age_band']= data['age_band'].fillna(100)

please help me

>Solution :

Try this shortcut:

data['age_band'] = data['age_band'].fillna(data['age'] // 10 * 10).astype(int)
print(data)

# Output
   age  age_band
0   93        90
1   46        40
2   50        50
3   56        50
4   89        80
5   19        10
6   25        20
7   17        10
8   54        50
9   42        40

Setup:

import pandas as pd
import numpy as np

np.random.seed(2022)
data = pd.DataFrame({'age': np.random.randint(1, 111, 10), 'age_band': np.nan})
print(data)

# Output
   age  age_band
0   93       NaN
1   46       NaN
2   50       NaN
3   56       NaN
4   89       NaN
5   19       NaN
6   25       NaN
7   17       NaN
8   54       NaN
9   42       NaN
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading