I'm creating columns with past data from the same columns of the database. Like for the same day, I need the Y
value of the day before and the same day of week in the week before. So:
x = df.copy()
x["Date"] = pd.to_datetime(df.rename(columns={"Año":"Year","Mes":"Month","Dia":"Day"})[["Year","Month","Day"]])
y = x[["Date","Y"]]
y.rename(columns={"Y":"Y_DiaAnterior"}, inplace=True)
y["Date"] = y["Date"] + dt.timedelta(days=1)
z = pd.merge(x,y,on=["Date"], how="left")
display(y.head()) # First merge result
a = x[["Date","Y"]]
a.rename(columns={"Y":"Y_DiaSemAnterior"}, inplace=True)
a["Date"] = a["Date"] + dt.timedelta(days=7)
z = pd.merge(x,a,on=["Date"], how="left")
z.head() # Second merge result
Where y df
is an auxiliar df
to create the column Y
with last day data, and a df
is an auxiliar df
to create the column Y
with same-day-last week data.
When I merge them separately it works perfectly, but when I want to merge all of them (first x with y and then x with a) the merge of x with y is 'deleted', as you can see that the Y_DiaAnterior
columns is not in the final df
(or 'second merge result') even when I already merged them.
So, how can I do that the final df
have Y_DiaAnterior
and Y_DiaSemAnterior
variables?