问题
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