做网站的公司 设计好,wordpress安装插件需要ftp,flash可以用来制作网页吗,保险网站建设优缺点这篇文章主要为大家详细介绍了python之DataFrame实现excel合并单元格#xff0c;具有一定的参考价值#xff0c;感兴趣的小伙伴们可以参考一下
在工作中经常遇到需要将数据输出到excel#xff0c;且需要对其中一些单元格进行合并#xff0c;比如如下表表格#xff0c;需要…这篇文章主要为大家详细介绍了python之DataFrame实现excel合并单元格具有一定的参考价值感兴趣的小伙伴们可以参考一下
在工作中经常遇到需要将数据输出到excel且需要对其中一些单元格进行合并比如如下表表格需要根据A列的值合并B、C列的对应单元格pandas中的to_excel方法只能对索引进行合并而xlsxwriter中虽然提供有merge_range方法但是这只是一个和基础的方法每次都需要编写繁琐的测试才能最终调好而且不能很好的重用。所以想自己写一个方法结合dataframe和merge_range。大概思路是
1、定义一个MY_DataFrame类继承DataFrame类这样能很好的利用pandas的很多特性而不用自己重新组织数据结构。
2、定义一个my_mergewr_excel方法参数分别为输出excel的路径、用于判断是否需要合并的key_cols列表、用于指明哪些列上的单元格需要被合并的列表
3、将MY_DataFrame封装为一个My_Module模块以备重用。
合并的算法如下
1、根据给定参数的【关键列】进行分组计数和排序添加CN和RN两个辅助列
2、判断CN大于1的该分组需要合并否则该分组行无需合并CN1说明这个分组数据行是唯一的无需合并
3、对应需要合并的分组判断当前列是不是在给定参数【合并列】中是则用合并写excel单元格否则就是普通的写excel单元格。
4、在需要合并的列中如果对于的RN1则调用merge_range一次性写想下写CN个单元格如果RN1则跳过该单元格因为在RN1的时候已经合并写了该单元格若再重复调用erge_range打开excel文档时会报错。
用图解释如下具体代码如下
# -*- coding: utf-8 -*-Created on 20170301
author: ARK-Zimport xlsxwriter
import pandas as pd
class My_DataFrame(pd.DataFrame):
def __init__(self, dataNone, indexNone, columnsNone, dtypeNone, copyFalse):
pd.DataFrame.__init__(self, data, index, columns, dtype, copy)
def my_mergewr_excel(self,path,key_cols[],merge_cols[]):
# sheet_nameSheet1, na_rep, float_formatNone, columnsNone, headerTrue, indexTrue, index_labelNone, startrow0, startcol0, engineNone, merge_cellsTrue, encodingNone, inf_repinf, verboseTrue):
self_copyMy_DataFrame(self,copyTrue)
line_cnself_copy.index.size
colslist(self_copy.columns.values)
if all([v in cols for i,v in enumerate(key_cols)])False: #校验key_cols中各元素 是否都包含与对象的列
print(key_cols is not completely include objects columns)
return False
if all([v in cols for i,v in enumerate(merge_cols)])False: #校验merge_cols中各元素 是否都包含与对象的列
print(merge_cols is not completely include objects columns)
return False
wb2007 xlsxwriter.Workbook(path)
worksheet2007 wb2007.add_worksheet()
format_top wb2007.add_format({border:1,bold:True,text_wrap:True})
format_other wb2007.add_format({border:1,valign:vcenter})
for i,value in enumerate(cols): #写表头
#print(value)
worksheet2007.write(0,i,value,format_top)
#merge_cols[B,A,C]
#key_cols[A,B]
if key_cols []: #如果key_cols 参数不传值则无需合并
self_copy[RN]1
self_copy[CN]1
else:
self_copy[RN]self_copy.groupby(key_cols,as_indexFalse).rank(methodfirst).ix[:,0] #以key_cols作为是否合并的依据
self_copy[CN]self_copy.groupby(key_cols,as_indexFalse).rank(methodmax).ix[:,0]
#print(self)
for i in range(line_cn):
if self_copy.ix[i,CN]1:
#print(该行有需要合并的单元格)
for j,col in enumerate(cols):
#print(self_copy.ix[i,col])
if col in (merge_cols): #哪些列需要合并
if self_copy.ix[i,RN]1: #合并写第一个单元格下一个第一个将不再写
worksheet2007.merge_range(i1,j,iint(self_copy.ix[i,CN]),j, self_copy.ix[i,col],format_other) ##合并单元格根据LINE_SET[7]判断需要合并几个
#worksheet2007.write(i1,j,df.ix[i,col])
else:
pass
#worksheet2007.write(i1,j,df.ix[i,j])
else:
worksheet2007.write(i1,j,self_copy.ix[i,col],format_other)
#print(,)
else:
#print(该行无需要合并的单元格)
for j,col in enumerate(cols):
#print(df.ix[i,col])
worksheet2007.write(i1,j,self_copy.ix[i,col],format_other)
wb2007.close()
self_copy.drop(CN, axis1)
self_copy.drop(RN, axis1)
调用代码
import My_Module
DFMy_DataFrame({A:[1,2,2,2,3,3],B:[1,1,1,1,1,1],C:[1,1,1,1,1,1],D:[1,1,1,1,1,1]})
DF
Out[120]:
A B C D
0 1 1 1 1
1 2 1 1 1
2 2 1 1 1
3 2 1 1 1
4 3 1 1 1
5 3 1 1 1
DF.my_mergewr_excel(000_2.xlsx,[A],[B,C])
效果如下也可以设置合并A、B列
DF.my_mergewr_excel(000_2.xlsx,[A],[A,B])
效果如下以上就是python之DataFrame实现excel合并单元格_python的详细内容更多请关注php中文网其它相关文章本文原创发布php中文网转载请注明出处感谢您的尊重