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...
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...
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