This
df1.loc[(df1['i1'] == 0) & (df1['time']== x),'i1'] = df2[df2['time']==x]['i1']
is returning NaNs because the indices between df2 and df1 don't align.
Advise on asking technical questions - don't provide screenshots, but instead provide the code to build df1 and df2. It is much easier for someone who is trying to help you to reproduce your issue
That being said here is my best attempt at an answer for you
In [2]: df1 = pd.DataFrame({
...: "time": [
...: pd.Timestamp("2018-01-01 00:00:00"),
...: pd.Timestamp("2018-01-01 00:00:00"),
...: pd.Timestamp("2018-01-01 00:00:00"),
...: pd.Timestamp("2010-01-01 00:00:10"),
...: pd.Timestamp("2010-01-01 00:00:10"),
...: pd.Timestamp("2010-01-01 00:00:10"),
...: ],
...: "indicator": [
...: 0, 1, 2, 0, 1, 2 ]
...: })
In [3]: df1
Out[3]:
time indicator
0 2018-01-01 00:00:00 0
1 2018-01-01 00:00:00 1
2 2018-01-01 00:00:00 2
3 2010-01-01 00:00:10 0
4 2010-01-01 00:00:10 1
5 2010-01-01 00:00:10 2
In [4]: df2 = df1.groupby("time").mean().reset_index()
In [5]: df2
Out[5]:
time indicator
0 2010-01-01 00:00:10 1.0
1 2018-01-01 00:00:00 1.0
In [6]: out = df1.merge(df2, on="time", suffixes=("_df1", "_df2")) # we merge to align the indices
In [7]: out
Out[7]:
time indicator_df1 indicator_df2
0 2018-01-01 00:00:00 0 1.0
1 2018-01-01 00:00:00 1 1.0
2 2018-01-01 00:00:00 2 1.0
3 2010-01-01 00:00:10 0 1.0
4 2010-01-01 00:00:10 1 1.0
5 2010-01-01 00:00:10 2 1.0
In [8]: out["indicator"] = out["indicator_df1"]
In [9]: mask = out["indicator_df1"] == 0
In [10]: out.loc[mask, "indicator"] = out.loc[mask, "indicator_df2"]
In [11]: out
Out[11]:
time indicator_df1 indicator_df2 indicator
0 2018-01-01 00:00:00 0 1.0 1
1 2018-01-01 00:00:00 1 1.0 1
2 2018-01-01 00:00:00 2 1.0 2
3 2010-01-01 00:00:10 0 1.0 1
4 2010-01-01 00:00:10 1 1.0 1
5 2010-01-01 00:00:10 2 1.0 2
What the above code does is it merges the source data with the data you want to impute and then performs the correction using a boolean mask. This will give you the correct answer and is considerably faster than running a for loop.
Note that this can be further simplified by relying on groupby.transform to avoid creating two dataframes and merging...