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