推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
yagamil
V2EX  ›  Python

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

  •  
  •   yagamil · May 29, 2021 · 2507 views
    This topic created in 1995 days ago, the information mentioned may be changed or developed.

    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 匹配不上。

    6 replies  •  2021-06-24 20:36:20 +08:00
    ryanchenn
    ryanchenn
    May 29, 2021❤️1
    1
    先 df1 = df1.reset_index(),合并之后再把 secID 那一列设为 index 。
    yagamil
    yagamil
    OP
    May 29, 2021
    2
    @ryanchenn 嗯,谢谢老师
    wctml
    wctml
    May 30, 2021
    3
    研究转债的什么机会
    ErenJaeger
    ErenJaeger
    Jun 2, 2021
    4
    pd.merge(df1,df2,on='tickerEqu', how='left')
    yagamil
    yagamil
    OP
    Jun 23, 2021
    5
    @wctml 老哥也玩转债?
    wctml
    wctml
    Jun 24, 2021
    6
    @yagamil 玩啊
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1929 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 11:51 · PVG 19:51 · LAX 03:51 · JFK 06:51
    ♥ Do have faith in what you're doing.