I am trying to replace NaN values with mean values

i have to replace the s_months and incidents NaN values with the corresponding means in jupyter notebook.
Input data :

    Types   c_years     o_periods   s_months    incidents
0   1       1           1           127.0       0.0
1   1       1           2           63.0        0.0
2   1       2           1           1095.0      3.0
3   1       2           2           1095.0      4.0
4   1       3           1           1512.0      6.0
5   1       3           2           3353.0      18.0
6   1       4           1           NaN         NaN
7   1       4           2           2244.0      11.0
14  2       4           1           NaN         NaN

I have tried the code below but it does not seem to work and I have tried different variations such as replacing the transform.

df.fillna['s_months'] = df.fillna(df.grouby(['types' , 'o_periods']['s_months','incidents']).tranform('mean'),inplace = True)
                 s_months  incidents
Types o_periods                     
1     1               911          3
      2              1688          8
2     1             26851         36
      2             14440         36
3     1               914          2
      2               862          1
4     1               296          0
      2               889          3
5     1               663          4
      2              1046          6

Answer

From your DataFrame :

>>> import pandas as pd
>>> from io import StringIO

>>> df = pd.read_csv(StringIO("""
Types,c_years,o_periods,s_months,incidents
0,1,1,1,127.0,0.0
1,1,1,2,63.0,0.0
2,1,2,1,1095.0,3.0
3,1,2,2,1095.0,4.0
4,1,3,1,1512.0,6.0
5,1,3,2,3353.0,18.0
6,1,4,1,NaN,NaN
7,1,4,2,2244.0,11.0
14,2,4,1,NaN,NaN"""), sep=',')
>>> df
    Types   c_years     o_periods   s_months    incidents
0   1       1           1           127.0       0.0
1   1       1           2           63.0        0.0
2   1       2           1           1095.0      3.0
3   1       2           2           1095.0      4.0
4   1       3           1           1512.0      6.0
5   1       3           2           3353.0      18.0
6   1       4           1           NaN         NaN
7   1       4           2           2244.0      11.0
14  2       4           1           NaN         NaN
>>> df[['c_years', 's_months', 'incidents']] = df.groupby(['Types', 'o_periods']).transform(lambda x: x.fillna(x.mean()))
>>> df
    Types   c_years     o_periods   s_months    incidents
0   1             1     1           127.000000      0.0
1   1             1     2           63.000000       0.0
2   1             2     1           1095.000000     3.0
3   1             2     2           1095.000000     4.0
4   1             3     1           1512.000000     6.0
5   1             3     2           3353.000000     18.0
6   1             4     1           911.333333      3.0
7   1             4     2           2244.000000     11.0
14  2             4     1           NaN             NaN

The last NaN is here because it belongs to the last group which contains no value in the columns s_months and incidents and therefore, no mean.