当前位置: 移动技术网 > IT编程>脚本编程>Python > python之DataFrame实现excel合并单元格

python之DataFrame实现excel合并单元格

2019年06月18日  | 移动技术网IT编程  | 我要评论

ps负片效果,杨省世父亲,书袋网

在工作中经常遇到需要将数据输出到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的,该分组需要合并,否则该分组(行)无需合并(cn=1说明这个分组数据行是唯一的,无需合并)
3、对应需要合并的分组,判断当前列是不是在给定参数【合并列】中,是则用合并写excel单元格,否则就是普通的写excel单元格。
4、在需要合并的列中,如果对于的rn=1则调用merge_range,一次性写想下写cn个单元格,如果rn>1则跳过该单元格,因为在rn=1的时候,已经合并写了该单元格,若再重复调用erge_range,打开excel文档时会报错。

用图解释如下:

具体代码如下:

# -*- coding: utf-8 -*- 
""" 
created on 20170301 
 
@author: ark-z 
""" 
import xlsxwriter 
 
 
import pandas as pd 
 
class my_dataframe(pd.dataframe): 
  def __init__(self, data=none, index=none, columns=none, dtype=none, copy=false): 
    pd.dataframe.__init__(self, data, index, columns, dtype, copy) 
 
  def my_mergewr_excel(self,path,key_cols=[],merge_cols=[]): 
    # sheet_name='sheet1', na_rep='', float_format=none, columns=none, header=true, index=true, index_label=none, startrow=0, startcol=0, engine=none, merge_cells=true, encoding=none, inf_rep='inf', verbose=true): 
    self_copy=my_dataframe(self,copy=true) 
    line_cn=self_copy.index.size 
    cols=list(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 object's 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 object's 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_index=false).rank(method='first').ix[:,0] #以key_cols作为是否合并的依据 
      self_copy['cn']=self_copy.groupby(key_cols,as_index=false).rank(method='max').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(i+1,j,i+int(self_copy.ix[i,'cn']),j, self_copy.ix[i,col],format_other) ##合并单元格,根据line_set[7]判断需要合并几个 
              #worksheet2007.write(i+1,j,df.ix[i,col]) 
            else: 
              pass 
            #worksheet2007.write(i+1,j,df.ix[i,j]) 
          else: 
            worksheet2007.write(i+1,j,self_copy.ix[i,col],format_other) 
          #print(',') 
      else: 
        #print('该行无需要合并的单元格') 
        for j,col in enumerate(cols): 
          #print(df.ix[i,col]) 
          worksheet2007.write(i+1,j,self_copy.ix[i,col],format_other) 
         
         
    wb2007.close() 
    self_copy.drop('cn', axis=1) 
    self_copy.drop('rn', axis=1) 


调用代码:

import my_module 
 
df=my_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']) 

效果如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网