推荐学习书目
› 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
yellowtail
V2EX  ›  Python

求教 pandas 相关问题,有一个表 A,对其中一列 a 进行 groupby 和 agg(比如 count)处理后得到新表 B,如何把 B 表聚合结果列映射到 A 表?

  •  
  •   yellowtail · Feb 8, 2022 · 3146 views
    This topic created in 1689 days ago, the information mentioned may be changed or developed.
    有一个表 A,对其中一列进行 groupby 和 agg(比如 sum)处理后得到新表 B,如何把 B 表聚合结果列映射到 A 表?

    比如表 A 是一个某地居民出生时间的表,索引是顺序时间( 2000-01-01 11:11 ),列值 datetime 对应的是日期加小时( 2000-01-01 11:00 )
    A.groupby['datetime'].count()得到表 B ,获得某天某小时出生人数
    请问如何快速在 A 表上新增一列,新列列值为 datetime 列值在 B 表中对应的出生人数?
    flyfei
        1
    flyfei  
       Feb 9, 2022 via Android
    使用 merge 方法设定为 left join 就可以吧
    milkpuff
        2
    milkpuff  
       Feb 9, 2022
    把 agg 换成 transform ,生成的结果与输入表有相同的行数。
    A.groupby['datetime'].transform('count')
    Nehcknarf
        3
    Nehcknarf  
       Feb 9, 2022
    A['new'] = A.groupby['datetime'].count()
    yellowtail
        4
    yellowtail  
    OP
       Feb 9, 2022
    @milkpuff 用你的方法解决问题了,感谢!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2358 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 05:33 · PVG 13:33 · LAX 22:33 · JFK 01:33
    ♥ Do have faith in what you're doing.