I have some nan cells in the year column. I thought it'll be better to set the Mode of the year column grouped-By Industry type.
df
ID Name Industry Year Employees Expenses Profit
1 E-Zim Health 2019 320 1,130,700 8553827
2 Daltfase Software NaN 78 804,035 13212508
3 Hotlane Government 2012 87 1,044,375 8701897
4 Latho Health NaN 103 4,631,808 10727561
5 Lambam IT Services 2015 210 4,374,841 4193069
6 Quozap Health 2008 21 4,626,275 8179177
7 Tampware Health 2008 13 2,127,984 3259485
For mode values, i did:
df_mode_year = df.groupby('Industry')['Year'].apply(lambda x: x.mode().iloc[0])
df_mode_year
Industry
Government 2012
Health 2008
IT Services 2015
Software
And then to modify my df, i tried
- df['Year'].fillna(df_mode_year)
- df['Year'] = df['Year'].fillna(df_mode_year[df['Industry']=='Health'])
But none of these two are affecting the final df .
Expected Output:
df
ID Name Industry Year Employees Expenses Profit
1 E-Zim Health 2019 320 1,130,700 8553827
2 Daltfase Software NaN 78 804,035 13212508
3 Hotlane Government 2012 87 1,044,375 8701897
4 Latho Health 2008 103 4,631,808 10727561
5 Lambam IT Services 2012 210 4,374,841 4193069
6 Quozap Health 2008 21 4,626,275 8179177
7 Tampware Health 2008 13 2,127,984 3259485
What am i doing wrong? Thanks a lot.