1

I have a pandas dataframe called 'df'. Its columns are id, age and sex. Some 'age' is missing. If age is missing I need to fill it using value from previous column.
For example , the age for id 20 must be 33.0 and age for id 50 must be 45.0. How do I do this using apply. ( I know this could be done using iloc/loc/iterrows. But I am looking to solve this using apply)

d = {'id': [10,20,30,40,50],'sex':['M','M','F','M','M'] ,'age': [33,np.nan,24,45,np.nan]}
df = pd.DataFrame(data=d)
df.set_index('id')


id  age   sex

10  33.0    M
20  NaN     M
30  24.0    F
40  45.0    M
50  NaN     M

===== Expected Result ===

id  age   sex

10  33.0    M
20  33.0    M
30  24.0    F
40  45.0    M
50  45.0    M
BENY
  • 317,841
  • 20
  • 164
  • 234
Biju Oommen
  • 151
  • 1
  • 2
  • 6

0 Answers0