I am trying to merge two dataframes based on the Date
column but in df1
the data column is titled Index
while the other is called Date
.
df1
Index | SMB | HML | RF |
---|---|---|---|
2018 | 2 | 3 | 4 |
2019 | 4 | 4 | 5 |
2020 | 4 | 5 | 2 |
df2
Date | ABC | DEF | GHI |
---|---|---|---|
2018 | 22 | 38 | 49 |
2019 | 41 | 42 | 59 |
2020 | 41 | 54 | 29 |
I have tried to set the index in DF1 but i keep getting the error message : "None of ['Index'] are in the columns"
This is the code I have tried:
df1 = df1.set_index('Index').T.set_index('Date').T
df1
data was imported if that changes anything. I would like to eventually merge the two dataframes so it looks something like this:
df3
Date | ABC | DEF | GHI | SMB | HML | RF |
---|---|---|---|---|---|---|
2018 | 22 | 38 | 49 | 2 | 3 | 4 |
2019 | 41 | 42 | 59 | 4 | 4 | 5 |
2020 | 41 | 54 | 29 | 4 | 5 | 2 |
CodePudding user response:
df3 = df2.merge(df1, right_on='Index', left_on='Date').drop('Index', axis=1)
Output:
>>> df3
Date ABC DEF GHI SMB HML RF
0 2018 22 38 49 2 3 4
1 2019 41 42 59 4 4 5
2 2020 41 54 29 4 5 2