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

Python3 Pandas 统计问题

  •  
  •   Kakarrot · 2018-12-20 18:25:40 +08:00 · 1365 次点击
    这是一个创建于 1951 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有什么办法可以确保加上labels参数后,输出的结果是准确的?

    result = pd.DataFrame(recharge_user).groupby(
            pd.cut(
                np.array(recharge_user),
                bins=[0, 10, 50, 100, 500, 1000, 3000, 5000, 10000, np.inf],
                # labels={
                #     '10 元以下人数:',
                #     '10-50 元人数:',
                #     '50-100 元人数:',
                #     '100-500 元人数:',
                #     '500-1000 元人数:',
                #     '1000-3000 元人数:',
                #     '3000-5000 元人数:',
                #     '5000-10000 元人数:',
                #     '10000 元以上人数:'
                # }
            )
        ).count()
        print(result)
    

    打印结果 1:正确!

    (0.0, 10.0]        1120
    (10.0, 50.0]        661
    (50.0, 100.0]       220
    (100.0, 500.0]      406
    (500.0, 1000.0]     108
    (1000.0, 3000.0]     92
    (3000.0, 5000.0]     15
    (5000.0, 10000.0]     5
    (10000.0, inf]        1
    

    打印结果 2:错误!

    10 元以下人数:        1120
    50-100 元人数:       661
    1000-3000 元人数:    220
    100-500 元人数:      406
    10000 元以上人数:      108
    500-1000 元人数:      92
    3000-5000 元人数:     15
    10-50 元人数:          5
    5000-10000 元人数:     1
    
    2 条回复    2018-12-20 19:02:36 +08:00
    neosfung
        1
    neosfung  
       2018-12-20 18:59:38 +08:00
    labels 不是应该是 list 么?
    imn1
        2
    imn1  
       2018-12-20 19:02:36 +08:00
    好像跟统计没什么关系
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5300 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 08:39 · PVG 16:39 · LAX 01:39 · JFK 04:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.