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
ErenJaeger
V2EX  ›  Python

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

  •  
  •   ErenJaeger · 2021-05-21 15:00:31 +08:00 · 1291 次点击
    这是一个创建于 1042 天前的主题,其中的信息可能已经有所发展或是发生改变。

    举个例子

    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 条回复    2021-05-24 08:59:22 +08:00
    Rush9999
        1
    Rush9999  
       2021-05-21 17:22:11 +08:00   ❤️ 1
    for cup,temp_df in df.groupby(['cup']) :
    temp_df.apply(hh,args=(cup))
    ErenJaeger
        2
    ErenJaeger  
    OP
       2021-05-21 17:55:07 +08:00
    @Rush9999 感谢,受教了
    yech1990
        3
    yech1990  
       2021-05-22 10:50:12 +08:00 via Android
    x.name 就是 group key,不要写循环。
    df.groupby("cup").apply(lambda x: hh(x, x.name))
    ErenJaeger
        4
    ErenJaeger  
    OP
       2021-05-24 08:59:22 +08:00
    @yech1990 谢谢老哥,查文档还没查到这一说
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1004 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 19:58 · PVG 03:58 · LAX 12:58 · JFK 15:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.