关于pandas多级表头,输出Excel文件空白行问题

找到pandas源码文件 excel.py 函数起始大概在源码551行, 贴上源码 函数_format_regular_rows

def _format_regular_rows(self):
        has_aliases = isinstance(self.header, (tuple, list, np.ndarray, Index))
        if has_aliases or self.header:
            self.rowcounter += 1

        # output index and index_label?
        if self.index:
            # check aliases
            # if list only take first as this is not a MultiIndex
            if self.index_label and isinstance(
                self.index_label, (list, tuple, np.ndarray, Index)
            ):
                index_label = self.index_label[0]
            # if string good to go
            elif self.index_label and isinstance(self.index_label, str):
                index_label = self.index_label
            else:
                index_label = self.df.index.names[0]

            if isinstance(self.columns, ABCMultiIndex):
                self.rowcounter += 1

            if index_label and self.header is not False:
                yield ExcelCell(self.rowcounter - 1, 0, index_label, self.header_style)

            # write index_values
            index_values = self.df.index
            if isinstance(self.df.index, ABCPeriodIndex):
                index_values = self.df.index.to_timestamp()

            for idx, idxval in enumerate(index_values):
                yield ExcelCell(self.rowcounter + idx, 0, idxval, self.header_style)

            coloffset = 1
        else:
            coloffset = 0

        for cell in self._generate_body(coloffset):
            yield cell

注释掉这段代码即可

#if isinstance(self.columns, ABCMultiIndex):
#	self.rowcounter += 1

其实在注释上已经解释了为什么会多一行空白,大概在 606行

# MultiIndex columns require an extra row
# with index names (blank if None) for
# unambiguous round-trip, unless not merging,
# in which case the names all go on one row Issue #11328
意思是多级表头 会多出一行 DEBUG 看到 控制参数为 self.rowcounter

至此问题已经解决.暂不清楚是否会有其他影响,希望下个版本能修复这个问题吧

经验分享 程序员 微信小程序 职场和发展