Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 修复了使用多重索引导出到Excel的问题_Python_Excel_Python 2.7_Pandas - Fatal编程技术网

Python 修复了使用多重索引导出到Excel的问题

Python 修复了使用多重索引导出到Excel的问题,python,excel,python-2.7,pandas,Python,Excel,Python 2.7,Pandas,到目前为止,这是我的问题。我的Pandas版本是0.15.2,我正在使用Python 2.7。我正在尝试将具有多索引列的数据框导出到excel,而不使用垂直索引。这充分概括了我的问题: # imports >>> import pandas as pd >>> import numpy as np # setting columns >>> level_one = ['a']*3 + ['b']*3 + ['c']*3 >>>

到目前为止,这是我的问题。我的Pandas版本是0.15.2,我正在使用Python 2.7。我正在尝试将具有多索引列的数据框导出到excel,而不使用垂直索引。这充分概括了我的问题:

# imports
>>> import pandas as pd
>>> import numpy as np
# setting columns
>>> level_one = ['a']*3 + ['b']*3 + ['c']*3
>>> level_two = ['1', '2', '3']*3
>>> columns = [np.array(level_one), np.array(level_two)]
# making the dataframe
>>> df = pd.DataFrame(index=range(10), columns=columns)
>>> series = pd.Series({k:k**2 for k in range(10)})
>>> df[first_col] = series
>>> first_col = df.columns.tolist()[0]
>>> df
     a              b              c          
     1    2    3    1    2    3    1    2    3
 0   0  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
 1   1  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
 2   4  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
 3   9  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
 4  16  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
 5  25  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
 6  36  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
 7  49  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
 8  64  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
 9  81  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
所以,到目前为止,一切正常。现在,当我导出到csv时,这两种方法都在数据完整性方面起作用(没有数据相对于标题的移动,尽管它会在一级中重复每个元素)

但是,对于ExcelWriter,它执行以下操作:

 >>> import os
 >>> path = os.path.join(os.path.expanduser('~'), 'test.xlsx')
 >>> writer = pd.ExcelWriter(path)
 >>> df.to_excel(writer, 'Sheet 1')
 >>> df.to_excel(writer, 'Sheet 2', index=False)
 >>> writer.save()


如您所见,它消除了索引,但不在列标题中,从而导致数据移动。是否有修复或解决方法?在我的实际数据集中,实际的“索引”有重复的元素和间隔符,因此不适合用作索引。

因此我在文档中找不到任何可以解决此问题的内容,但我提出了一个临时修复方案

import itertools

def pseudo_header(df):
    '''Create a pseudo-header for the dataframe due to indexing
    issues.
    '''

    # grab grouped columns
    columns = df.columns
    grouped = itertools.groupby(columns, key=lambda x: x[0])
    grouped = [tuple(v) for k, v in grouped]
    # grab col/index counters
    index = DF.get_last_index(df)
    counter = 0
    # set rows
    df.loc[index] = pd.Series(index=columns)
    df.loc[index+1] = pd.Series(index=columns)
    for group in grouped:
        for idx, values in enumerate(group):
            # grab indexing
            column = columns[counter]
            if idx == 0:
            df.loc[index, column] = values[0]
            df.loc[index+1, column] = values[1]
            counter += 1
然后,我在索引和标题关闭的情况下导出。它可以工作,没有标题格式,但这是一个不错的修复


目前,它只适用于2级标题,我可以进行概括,但出于我的需要,现在不需要它。

还要补充:如果我有多索引索引索引和列,也会发生这种情况,但偏移量仍然只有一个。简而言之,尽管索引现在占用“n”列而不是1列,但输出是完全相同的。我也在所有3个引擎(XlsxWriter、OpenPyXl和Xlwt)中尝试过这一点,并且所有3个引擎的错误都是完全相同的。我应该提交错误报告吗?
import itertools

def pseudo_header(df):
    '''Create a pseudo-header for the dataframe due to indexing
    issues.
    '''

    # grab grouped columns
    columns = df.columns
    grouped = itertools.groupby(columns, key=lambda x: x[0])
    grouped = [tuple(v) for k, v in grouped]
    # grab col/index counters
    index = DF.get_last_index(df)
    counter = 0
    # set rows
    df.loc[index] = pd.Series(index=columns)
    df.loc[index+1] = pd.Series(index=columns)
    for group in grouped:
        for idx, values in enumerate(group):
            # grab indexing
            column = columns[counter]
            if idx == 0:
            df.loc[index, column] = values[0]
            df.loc[index+1, column] = values[1]
            counter += 1