Sjoseph :
I have two dataframes (dfA and dfB) with a sample from both given below. I want to join the dataframes to produce the Result given
dfA
Id, year, B, D
1, 2010, 15, 33
1, 2011, 24, 72
1, 2012, 30, 16
dfB
Id, year, A, C
1, 2009, 100, 1
1, 2010, 75, 7
1, 2012, 60, 3
1, 2013, 42, 4
Result
Id, year, A, B, C, D
1, 2009,100, 0, 1, 0
1, 2010,75,15, 7, 33
1, 2011,0, 24, 0, 72
1, 2012,60, 30, 3, 16
1, 2013,42, 0, 4, 0
Attempt
I have experimented with pandas.merge trying inner, outer, left and right joins but am unable to get the desired result!
result = pd.merge(dfA,dfB,on=['Id','year'], how = 'outer')
Any tips would be greatly appreciated!
YOBEN_S :
merge
has the correct output , we just need to order and sort_values
s=pd.merge(df1,df2,on=['Id','year'], how = 'outer').\
sort_index(level=0,axis=1).sort_values(['Id', 'year']).fillna(0)
s
Out[81]:
A B C D year Id
3 100.0 0.0 1.0 0.0 2009 1
0 75.0 15.0 7.0 33.0 2010 1
1 0.0 24.0 0.0 72.0 2011 1
2 60.0 30.0 3.0 16.0 2012 1
4 42.0 0.0 4.0 0.0 2013 1