• 技术文章 >Python技术 >Python基础教程

    如何使用Pandas处理Excel?

    十一十一2021-01-08 11:19:08原创2518

    做过行政或者人事,或者对此有过了解的小伙伴,一定对下发各个部分的表有着非常深刻的印象,最常见的就是需要我们将一个总表,处理成一个一个单个的表,然后进行每个部门的下发,在编程中,需要将多个工作表的拆分与合并,始终在一个工作簿内操作。我们需要通过Pandas库来实现。

    调用工具:

    groupby()方法

    Excel的追加模式

    Pandas

    实现方式:

    采用函数、面向对象过程编写

    实现结果:

    将部门生成工作表

    groupby()方法用法:

    grouped = df.groupby('department')
    print(grouped.get_group('技术部'))
    for name,group in df.groupby('department'):
    print(name,group)

    工作表拆分:

    import pandas as pd
    import os
    curpath = os.path.dirname(__file__)
    filename = os.path.join(curpath, 'example_merge.xlsx')
    savefilename = os.path.join(curpath, 'example_merge_1.xlsx')
    df=pd.read_excel(filename)
    writer = pd.ExcelWriter(savefilename,engine='openpyxl', mode='a')
    for name,group in df.groupby('department'):
    group.to_excel(writer,name)
    writer.save()

    生成结果:

    python中有句俗话是“使用Pandas处理Excel,节省大量代码,谁用谁知道!”,大家可以实际操作起来啦。

    专题推荐:pandas处理excel
    品易云
    上一篇:python中的win32com库是什么? 下一篇:python如何实现工作表合并?

    相关文章推荐

    • Python中Selenium库如何调用浏览器?• python中的win32com库是什么?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网