0

How can I inject values of dataframe and complement NaN of series with different length like those:

series             dataframe
                           0
0      NaN       0  0.468979
1      3.0       1  0.470546
2      NaN       2  0.458234
3      3.0       3  0.427878
4      NaN       4  0.494763
       ...          ...
886    NaN     682  0.458234
887    2.0     683  0.501460
888    NaN     684  0.458234
889    3.0     685  0.494949
890    NaN     686  0.427878
891    2.0
892    3.0
893    1.0

I need to find something that can inject values of dataframe to NaN of series like below:

0    0.468979  <- row0 of dataframe
1         3.0  
2    0.470546  <- row1 of dataframe
3         3.0
4    0.458234  <- row2 of dataframe
     ...
886  0.458234  <- row684 of dataframe
887       2.0
888  0.494949  <- row685 of dataframe
889       3.0
890  0.427878  <- row686 of dataframe
891       2.0
892       3.0
893       1.0

Actually, I can get the above result by this code:

j = 0
for i, c in enumerate(series):
    if np.isnan(c):
        series[i] = dataframe[0][j]
        j += 1

but it makes SettingWithCopyWarning.

How can I inject values of dataframe and complement NaN of series without the warning?

According to the previous question: How to remove nan value while combining two column in Panda Data frame? I tried below, however, it doesn't work well due to the different length:

series = series.fillna(dataframe[0])

0      0.468979  <- row0 of dataframe
1      3.000000
2      0.458234  <- row2 of dataframe
3      3.000000
4      0.494763  <- row4 of dataframe
         ...
886         NaN
887    2.000000
888         NaN
889    3.000000
890         NaN
891    2.000000
892    3.000000
893    1.000000

0 Answers0