V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
leisurelylicht
V2EX  ›  Python

请问 pandas 的 left join 方法为什么会增加原有数据的行?

  •  
  •   leisurelylicht · 2017-10-12 17:18:01 +08:00 · 4463 次点击
    这是一个创建于 2359 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我用 pandas 的 join 方法拼接数据,代码如下

    package_item_df = package_item_df.join(res_df, on=['whistler_order_id', 'order_item_id', 'order_item_sn'])

    结果拼完以后 package_item_df 的行数增加了,

    join 默认是 left join,

    应该是以调用 join 的 dataframe 数据为准,为什么会改变原有 dataframe 的行数?
    3 条回复    2017-10-13 17:20:17 +08:00
    flyws
        1
    flyws  
       2017-10-12 20:24:21 +08:00
    有没有可能是 join 的 data 里面的列不唯一,也就是 package_item_df 里面有些一行数据对应了右边表多条数据,这样出来可能会增加行数。
    snowolfy
        2
    snowolfy  
       2017-10-13 10:04:48 +08:00
    pandas 中的`df.join`并不是对等于关系型数据库中的 join, 而是把两个 DF 拼起来。`how` 参数只是制定了拼起来的结果使用哪边的`index`。

    可能楼主想要的功能是`df.merge`.
    `df.join`: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.join.html
    `df.merge`: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.merge.html
    还有这个: https://pandas.pydata.org/pandas-docs/stable/merging.html
    leisurelylicht
        3
    leisurelylicht  
    OP
       2017-10-13 17:20:17 +08:00
    @flyws 多谢,真是这样。先对右面做聚合就好了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4537 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 10:07 · PVG 18:07 · LAX 03:07 · JFK 06:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.