I have two data-frames as shown below (sample data) :
df1:
Key1 Val1 Val2 Val3
A 2 1 3
B 2 2 2
C 3 3 4
D 4 4 2
E 5 6 5
df2:
Key1 Seasionality
A 1.2
B -1
C 1
D 1.2
E 1.5
How can we add Seasonality
to df1 (val1, val2 , val3) where df1.key1 = df2.key1
.
Desired Output:
Key1 Val1 Val2 Vale
A 3.2 2.2 4.2
B 1 1 1
C 4 4 5
D 5.2 5.2 3.2
E 6.5 7.5 6.5
Previously i had similar requirement where i need to add columns from two different df with same keys but they had similar column names, so i was able to use :
pd.concat([df1,df2]).groupby(level=0).sum()
But can not think of a solution to above problem.
One possible solution is to align the keys temporarily:
df1.set_index('Key1', inplace=True)
df2.set_index('Key1', inplace=True)
df1 = df1.add(df2['Seasionality'], axis=0)
df1.reset_index(inplace=True)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.