How to merge four pandas dataframes horizontally based on a rank column?
I have four dataframes_
dfOne
co name rank
AA a 1
RF b 2
GR c 3
AS d 4
dfTwo
co name rank
FG h 1
HT j 2
RD r 3
AR y 4
dfThree
rank place
2 dhht
3 hgd
4 rfn
5 vdr
dfFour
rank place
1 ghr
2 dvf
3 rdn
4 mki
How can I merge these four dataframes horizontally based on rank column
My expectation is like this_
df
place co name rank name co place
dhht AA a 1 h FG ghr
hgd RF b 2 j HT dvf
rfn GR c 3 r RD rdn
- AS d 4 y AR mki
vdr - - 5 - - -
CodePudding user response:
You can set rank
as index temporarily and concat
horizontally:
dfs = [dfOne, dfTwo, dfThree, dfFour]
out = pd.concat([d.set_index('rank') for d in dfs], axis=1).reset_index()
output:
rank co name co name place place
0 1 AA a FG h NaN ghr
1 2 RF b HT j dhht dvf
2 3 GR c RD r hgd rdn
3 4 AS d AR y rfn mki
4 5 NaN NaN NaN NaN vdr NaN
CodePudding user response:
You can use:
from functools import reduce
df_list = [dfOne, dfTwo, dfThree, dfFour]
df_result = reduce(lambda left,right: pd.merge(left,right,on='rank', how='outer'), df_list)
I have found this here: Python: pandas merge multiple dataframes