Joining multiple dataframes together and making sure the right numbers go to the same column

梦想的初衷 提交于 2020-07-09 14:42:09

问题


I have a few dataframes that all have a "Dirty Price" and "Factor" that I want to have in the same column. When I try to do this what happens when I join the second dataframe together then pandas creates a new "Dirty Price_x" and "Factor_x" but I don't want new columns I want them to me merged into the same column that match the criteria I specify.

This is what I did:

df = df.merge(gs[['CUSIP', 'temp_Counterparty','Dirty Price','Factor']], how = 'left', on = ['CUSIP', 'temp_Counterparty'])
df = df.merge(nomura[['CUSIP', 'temp_Counterparty','Dirty Price','Factor']], how = 'left', on = ['CUSIP', 'temp_Counterparty'])

Is there something I am doing wrong here?


回答1:


Since gs and nomura do not share the same combination of CUSIP and Counterpary, you can concat these two dataframes and then do the merge

df_concat = pd.concat([ gs[['CUSIP', 'temp_Counterparty','Dirty Price','Factor']] , 
                        nomura[['CUSIP', 'temp_Counterparty','Dirty Price','Factor']]
                      ], axis = 0)


df = df.merge(df_concat, how='left', on=['CUSIP', 'temp_Counterparty'])


来源:https://stackoverflow.com/questions/61347810/joining-multiple-dataframes-together-and-making-sure-the-right-numbers-go-to-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!