如何使用Pandas处理Excel?

611次阅读
没有评论
如何使用Pandas处理Excel?

做过行政或者人事,或者对此有过了解的小伙伴,一定对下发各个部分的表有着非常深刻的印象,最常见的就是需要我们将一个总表,处理成一个一个单个的表,然后进行每个部门的下发,在编程中,需要将多个工作表的拆分与合并,始终在一个工作簿内操作。我们需要通过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,节省大量代码,谁用谁知道!”,大家可以实际操作起来啦。

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-07-18发表,共计754字。
新手QQ群:570568346,欢迎进群讨论 Python51学习