I have a dataframe with columns stud_id,stud_class,stud_mark. some values in stud_mark as marked as NaN. My requirement is to fill the NaN values with average of marks obtained from each class. while doing so the NaN value is not changing.
import pandas as pd
import numpy as np
data=({'stud_id': [1, 2, 3, 4, 5],
'stud_class': ['1st', '1st', '1st', '2nd', '2nd'],
'stud_mark': [100, np.nan, 90, np.nan, 80]})
df=pd.DataFrame(data)
print(df)
# Calculate the average mark for each class
average_mark = df.groupby('stud_class')['stud_mark'].mean()
# Fill the blank marks with the average mark for each class
df['stud_mark'] = df['stud_mark'].fillna(average_mark)
print(df)
output
stud_id stud_class stud_mark
0 1 1st 100.0
1 2 1st NaN
2 3 1st 90.0
3 4 2nd NaN
4 5 2nd 80.0
Need some input on this to resolve.