0

I have 2 pandas dataframes.

Both df(s) have a date column.

I want to copy a column ['Volume'] from df_2 to df_1 where the df_1['date'] = df_2['date'].

I can loop through the 1st df, but after that, I'm lost...

BeRT2me
  • 12,699
  • 2
  • 13
  • 31
  • 1
    Does this answer your question? [Pandas Merging 101](https://stackoverflow.com/questions/53645882/pandas-merging-101) – BeRT2me Aug 27 '22 at 01:10

1 Answers1

1

Use df1.merge(df2[columns], how='inner', on='Date'). For Example:

df1 = pd.DataFrame({'Date': ['Monday', 'Tuesday'], 'Random': [1, 2]})
df2 = pd.DataFrame({'Date': ['Monday', 'Wednesday'], 'Volume': [3, 4]})

print(df1.merge(df2[['Date', 'Volume']], how='inner', on='Date'))

Output:

     Date  Random  Volume
0  Monday       1       3

Source: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.merge.html

Jeffrey Ram
  • 1,132
  • 1
  • 10
  • 16