7

pandas 合并两个表,如何保留第一个表的索引?

 3 years ago
source link: https://www.v2ex.com/t/780071
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  Python

pandas 合并两个表,如何保留第一个表的索引?

  yagamil · 15 小时 28 分钟前 · 324 次点击

df1 数据

      tickerBond  closePriceBond  bondPremRatio secShortNameBond tickerEqu  \
secID                                                                         
110066     110066          199.94        -1.2442             盛屯转债    600711   
110067     110067          119.53        25.9204             华安转债    600909   
113021     113021          105.81        45.0989             中信转债    601998   
113024     113024          101.94        36.6668             核建转债    601611   
113025     113025          129.16         0.0409             明泰转债    601677

df2 数据

        ROE tickerEqu
0  2.642931    600711
1  4.425438    600909
2  6.259092    601998
3  4.432315    601611
4  6.454054    601677

如果按照 pd.merge(df1,df2,on='tickerEqu') ,按照列 tickerEqu 进行合并,这样会导致最后合成的新的列的索性重构,变成 0,1,2,3 这种的。

有什么办法可以保留 df1 的索引? 用 join 的话会报错,因为 df2 的索引和 df1 匹配不上。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK