将多个数据框连接在一起,并确保正确的数字进入同一列 [英] Joining multiple dataframes together and making sure the right numbers go to the same column

查看:179
本文介绍了将多个数据框连接在一起,并确保正确的数字进入同一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据框,它们都希望在同一列中包含脏价"和因子".当我尝试执行此操作时,当我将第二个数据框连接在一起时会发生什么,然后pandas创建了一个新的"Dirty Price_x"和"Factor_x",但是我不希望新列合并到符合条件的同一列中我指定.

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.

这就是我所做的:

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?

推荐答案

由于gsnomura不共享CUSIPCounterpary的相同组合,因此可以合并这两个数据帧,然后执行合并

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'])

这篇关于将多个数据框连接在一起,并确保正确的数字进入同一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆