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

pandas 分组后如何去后百分之 80 的值

  •  
  •   sznewbee096 · 2020-09-12 17:56:22 +08:00 · 2018 次点击
    这是一个创建于 1324 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在练习的时候,python 数据处理用 pandas.groupby 分组后取后百分之 80 的值,tail 只能取具体的后几行。

    8 条回复    2020-09-17 13:30:29 +08:00
    xzpjerry731
        1
    xzpjerry731  
       2020-09-12 18:05:03 +08:00
    用类似这种? df.groupby('xx').apply(lambda x: x.nsmallest(int(len(x) * 80), 'xxx')
    kangsheng9527
        2
    kangsheng9527  
       2020-09-12 18:08:29 +08:00
    shell 中 tail 对应另外相反一个命令是 head 。
    用到 python 了就直接 python 代码上处理了。。。
    sznewbee096
        3
    sznewbee096  
    OP
       2020-09-12 18:37:28 +08:00
    @xzpjerry731 试了一下,思路是没有问题,就是结果行数并未减少。不过谢谢了
    sznewbee096
        4
    sznewbee096  
    OP
       2020-09-12 18:39:08 +08:00
    @kangsheng9527 嗯嗯,tail 是取后几行,head 是去前几行,不过这个问题不知到有多少行,只取后百分之 80 。前提是分组后存在多列,比较麻烦。不分组的话,我还能处理
    xzpjerry731
        5
    xzpjerry731  
       2020-09-12 18:50:34 +08:00
    @sznewbee096 #3 发现 1 楼打错了,80 应该是 0.8 才对; df.groupby('xx').apply(lambda x: x.nsmallest(int(len(x) * 0.8), 'xxx')
    sznewbee096
        6
    sznewbee096  
    OP
       2020-09-12 19:08:44 +08:00
    @xzpjerry731 嗯嗯,刚刚我已经改成 0.8,但是取出的行数并不是最后的 80%
    sznewbee096
        7
    sznewbee096  
    OP
       2020-09-12 19:13:53 +08:00
    @sznewbee096 不好意思我运行出了问题,结果是对的。感谢。
    necomancer
        8
    necomancer  
       2020-09-17 13:30:29 +08:00
    a[a.shape[0]*8//10:]?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3107 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 13:39 · PVG 21:39 · LAX 06:39 · JFK 09:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.