Code to generate random database for question (minimum reproducible issue):
df_random = pd.DataFrame(np.random.random((2000,3)))
df_random['order_date'] = pd.date_range(start='1/1/2015', 
periods=len(df_random), freq='D')
df_random['customer_id'] = np.random.randint(1, 20, df_random.shape[0])
df_random
Output df_random
        0          1               2    order_date  customer_id
0   0.018473    0.970257    0.605428    2015-01-01    12
    ... ... ... ... ... ...
    1999    0.800139    0.746605    0.551530    2020-06-22  11
Code to extract mean unique transactions month and year wise
for y in (2015,2019):
for x in (1,13):
    df2 = df_random[(df_random['order_date'].dt.month == x)&(df_random['order_date'].dt.year== y)]
    df2.sort_values(['customer_id','order_date'],inplace=True)
    df2["days"] = df2.groupby("customer_id")["order_date"].apply(lambda x: (x - x.shift()) / np.timedelta64(1, "D"))
    df_mean=round(df2['days'].mean(),2)
    data2 = data.append(pd.DataFrame({'Mean': df_mean , 'Month': x, 'Year': y}, index=[0]), ignore_index=True)
    print(data2)
Expected output
  Mean         Month  Year
0   5.00          1  2015
    .......................
11  6.62         12  2015
..............Mean values of days after which one transaction occurs in order_date for years 2016 and 2017 Jan to Dec
36  6.03          1  2018
..........................
47  6.76         12  2018
48  8.40          1  2019
.......................
48  8.40         12  2019
Basically I want single dataframe starting from 2015 Jan month to 2019 December
Instead of the expected output I am getting dataframe from Jan 2015 to Dec 2018 , then again Jan 2015 data and then the entire dataset repeats again from 2015 to 2018 many more times.
Please help
 
     
    