V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Sep 12, 2020 · 2856 views
    This topic created in 2066 days ago, the information mentioned may be changed or developed.

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

    8 replies    2020-09-17 13:30:29 +08:00
    xzpjerry731
        1
    xzpjerry731  
       Sep 12, 2020
    用类似这种? df.groupby('xx').apply(lambda x: x.nsmallest(int(len(x) * 80), 'xxx')
    kangsheng9527
        2
    kangsheng9527  
       Sep 12, 2020
    shell 中 tail 对应另外相反一个命令是 head 。
    用到 python 了就直接 python 代码上处理了。。。
    sznewbee096
        3
    sznewbee096  
    OP
       Sep 12, 2020
    @xzpjerry731 试了一下,思路是没有问题,就是结果行数并未减少。不过谢谢了
    sznewbee096
        4
    sznewbee096  
    OP
       Sep 12, 2020
    @kangsheng9527 嗯嗯,tail 是取后几行,head 是去前几行,不过这个问题不知到有多少行,只取后百分之 80 。前提是分组后存在多列,比较麻烦。不分组的话,我还能处理
    xzpjerry731
        5
    xzpjerry731  
       Sep 12, 2020
    @sznewbee096 #3 发现 1 楼打错了,80 应该是 0.8 才对; df.groupby('xx').apply(lambda x: x.nsmallest(int(len(x) * 0.8), 'xxx')
    sznewbee096
        6
    sznewbee096  
    OP
       Sep 12, 2020
    @xzpjerry731 嗯嗯,刚刚我已经改成 0.8,但是取出的行数并不是最后的 80%
    sznewbee096
        7
    sznewbee096  
    OP
       Sep 12, 2020
    @sznewbee096 不好意思我运行出了问题,结果是对的。感谢。
    necomancer
        8
    necomancer  
       Sep 17, 2020
    a[a.shape[0]*8//10:]?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5599 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 05:51 · PVG 13:51 · LAX 22:51 · JFK 01:51
    ♥ Do have faith in what you're doing.