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

pandas 操作求助,数据如下

  •  
  •   Scorpiocat · 2020-12-14 13:01:33 +08:00 · 1700 次点击
    这是一个创建于 1223 天前的主题,其中的信息可能已经有所发展或是发生改变。
    import numpy as np
    import pandas as pd

    df = pd.DataFrame({'月':['1','2','3','5','7'],
    '医院':['人民','二院','人民','二院','人民'],
    '销量':np.arange(1,10,2) })
    print(df)

    df1 = df.groupby(['医院','月']).sum()
    print(df1)

    list = np.arange(1,8,1)
    print (list)

    #需求:groupby 之后,同一医院的月份没有在 list 中出现(比如人民没有 2,4,5,6 月),则补充缺失月份,并且销量数据为 0,同理下面的医院也进行同样操作
    10 条回复    2020-12-14 16:22:56 +08:00
    Scorpiocat
        2
    Scorpiocat  
    OP
       2020-12-14 13:46:47 +08:00
    谢谢回答!我运行了一下代码没有问题。但是怎么能把医院中缺失的月份数据补齐去呢?
    @noqwerty
    xyd1205148795
        3
    xyd1205148795  
       2020-12-14 13:48:01 +08:00
    这个可以吗
    df1 = df.set_index(['月','医院']).unstack()
    month =pd.DataFrame(np.arange(1,8,1).T,columns=['月'])
    month['月'] = month['月'].astype('str')
    data = month.merge(df1,how='left',on='月').fillna(0)
    print(data)
    jyyx
        4
    jyyx  
       2020-12-14 14:20:06 +08:00
    df = pd.DataFrame({'月':['1','2','3','5','7'],
    '医院':['人民','二院','人民','二院','人民'],
    '销量':np.arange(1,10,2) })
    month_list = ['1','2','3','4','5','6','7','8']
    df['月'] = df['月'].astype('category').cat.set_categories(month_list, ordered=True)
    gb = df.groupby(['医院','月']).sum().fillna(0)
    print(gb)
    princelai
        5
    princelai  
       2020-12-14 14:21:58 +08:00
    from itertools import product
    df2 = df1.reindex(index=list(product(df1.index.get_level_values(0).unique(),[f'{i}' for i in np.arange(1,8,1)])),fill_value=0)
    Scorpiocat
        6
    Scorpiocat  
    OP
       2020-12-14 14:46:08 +08:00
    我的想法是先 groupbysum,再对每一家医院的缺失月份数据补齐,这一步就不太明白。
    放出原始数据,看看大家有没有兴趣整理一下。

    原始数据
    链接: https://pan.baidu.com/s/1zVaTeBDGzL1q_DjYrHptIQ 提取码: 1d2j
    Scorpiocat
        7
    Scorpiocat  
    OP
       2020-12-14 15:51:57 +08:00
    princelai
        8
    princelai  
       2020-12-14 16:05:38 +08:00   ❤️ 1
    import pandas as pd

    df = pd.read_excel("data.xlsx")

    df1 = df.pivot_table(index="医院",columns="月",values="总计",aggfunc="sum").reindex(columns=range(1,13)).fillna(0).cumsum(axis=1)
    df1.columns = [f"{c}月" for c in df1.columns]
    princelai
        9
    princelai  
       2020-12-14 16:06:04 +08:00   ❤️ 2
    你这个 excel 表就能很快实现,一共也没多少数据
    Scorpiocat
        10
    Scorpiocat  
    OP
       2020-12-14 16:22:56 +08:00
    @princelai #9 厉害!谢谢!自己刚开始学,就想着实战了...
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2615 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 10:53 · PVG 18:53 · LAX 03:53 · JFK 06:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.