0

I'm trying to merge two dataframes in Pandas, but I end up with only the information from the calling dataframe.

Here's my minimal working example:

import pandas as pd
from datetime import date, timedelta

today = date.today()
yesterday = today - timedelta(days=1)
day_before = yesterday - timedelta(days=1)

df1 = pd.DataFrame({'date': [day_before, yesterday, today], 
                   'countdown': [3, 2, 1]})

df2 = pd.DataFrame({'date': [day_before, yesterday, today], 
                   'countup': [1, 2, 3]})

df1.merge(df2, on='date')

print(df1)

Output:

         date  countdown
0  2023-06-13          3
1  2023-06-14          2
2  2023-06-15          1

What I expected to see was:

         date  countdown  countup
0  2023-06-13          3        1
1  2023-06-14          2        2
2  2023-06-15          1        3

What am I doing wrong?

  • This works fine, but you should do `out = df1.merge(df2, on='date') ; print(out)` not `print(df1)`. [`merge`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.merge.html) is not in place. – mozway Jun 15 '23 at 12:17

0 Answers0