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

pandas 如何将分组的 value 传入 apply 函数中

  •  
  •   ErenJaeger · May 21, 2021 · 2756 views
    This topic created in 1953 days ago, the information mentioned may be changed or developed.

    举个例子

    In [4]: df
    Out[4]:
       name  age cup
    0   hah   18   A
    1  wuwu   19   B
    2  bibi   20   C
    3  diid   21   D
    
    def hh(df, cup_value):
    	...
    	return 
    
    

    我想按 cup,分组如何将 cup 的值传入 apply 函数中?除去在 apply 函数中根据 df 获取

    4 replies  •  2021-05-24 08:59:22 +08:00
    Rush9999
        1
    Rush9999  
       May 21, 2021   ❤️ 1
    for cup,temp_df in df.groupby(['cup']) :
    temp_df.apply(hh,args=(cup))
    ErenJaeger
        2
    ErenJaeger  
    OP
       May 21, 2021
    @Rush9999 感谢,受教了
    yech1990
        3
    yech1990  
       May 22, 2021 via Android
    x.name 就是 group key,不要写循环。
    df.groupby("cup").apply(lambda x: hh(x, x.name))
    ErenJaeger
        4
    ErenJaeger  
    OP
       May 24, 2021
    @yech1990 谢谢老哥,查文档还没查到这一说
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2526 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 07:30 · PVG 15:30 · LAX 00:30 · JFK 03:30
    ♥ Do have faith in what you're doing.