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

pandas 新手,使用 pd.cut 怎么自定义开闭区间?

  •  
  •   HashV2 · 2022-10-08 15:29:50 +08:00 · 1533 次点击
    这是一个创建于 537 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在用pd.cut(pd.Series, bins=bins, right=False).value_counts()对某一列值的区间数量进行统计

    我想要左闭右开,但是最后一个区间要左右都是闭区间, 也就是这样

    • [0, 100), [100, 200), [200, 300),...,[900, 1000]

    查阅了文档,只能左开右闭然后加上include_lowest=True让第一个参数的左区间变为闭区间,想了解一下有没有right=False, include_largest=True这样的实现方式

    文档

    3 条回复    2022-10-08 17:17:13 +08:00
    TimePPT
        1
    TimePPT  
       2022-10-08 16:12:40 +08:00
    蹲一个解决方案,我之前也遇到这个问题,最后因为都是整数,所以取巧把分箱最大值改成 1000.0000001 了😂
    HashV2
        2
    HashV2  
    OP
       2022-10-08 16:31:39 +08:00   ❤️ 1
    @TimePPT #1 没找到合适的方案,不过我的区间数值本来就是对列全覆盖的,bins 最后一个值可以给 np.inf ,然后替换 labels ,勉强算是实现了吧

    bins = [0, 100, 200,....900, np.inf]
    labels=['[0, 100)', '[100, 200)', ...., '[900, 1000]']

    pd.cut(pd.Series, bins=bins, right=False,labels=labels).value_counts(sort=False)
    Eureka0
        3
    Eureka0  
       2022-10-08 17:17:13 +08:00
    给 pandas 提 issue 加个 include_highest 参数(狗头
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2723 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 00:22 · PVG 08:22 · LAX 17:22 · JFK 20:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.