数据框 pandas excel 合并 header

dataframe pandas excel combine header

是否有可能 pandas 将两个(或三个...) header-rows 与联合 excel-cells 合并为一个? 例如 table:

| Report for .....                                           | Income                       | Ordered                                   |
|-----------------|--------------------|---------------------|--------------|---------------|-----------|-------------------------------|
| Brend           | Art of supplier    | Contract            | pcs          | income price  | pcs       | rub (prize for selling)       |
| Elena Chezelle  | Y0060              | 0400-6752 Agent     | 85           | 245,00        | 226       | 785,00                        |
| Amour Bridal    | ALWE-1199-WHITE    | 0400-6752 Agent     | 47           | 56,00         | 163       | 857,00                        |

进入:

| Brend          | Art of supplier    | Contract            | Income  pcs     | Income price              | Ordered pcs | Ordered rub (prize for selling)        |
|----------------|--------------------|---------------------|-----------------|---------------------------|-------------|----------------------------------------|
| Elena Chezelle | Y0060              | 0400-6752 Agent     | 85              | 245,00                    | 226         | 785,00                                 |
| Amour Bridal   | ALWE-1199-WHITE    | 0400-6752 Agent     | 47              | 56,00                     | 163         | 857,00                                 |

直到现在使用循环代码完成:

    file = 'static/ExportToEXCELOPENXML - 2020-05-11T180206.635.xlsx'

    df = pd.read_excel(file)

    df_list = []
    for x in df.columns:
        if 'Unnamed' in x or 'Report' in x:
            df_list.append('')
        else:
            df_list.append(x + ' ')

    i = 0
    while i < len(df_list):
        if df_list[i] == '':
            df_list[i] = df_list[i - 1]
        i += 1

    print(df_list)

    df.columns = df_list + df.iloc[0]
    df_new = df.iloc[1:].reset_index(drop=True)
    df_new.to_excel('static/test.xlsx', index=None)

是否可以只使用 pandas(没有循环)?

当您阅读 excel 时,您可以添加两行作为 header df = pd.read_excel('static/ExportToEXCELOPENXML - 2020-05-11T180206.635.xlsx', header=[0,1])

如果你想用分隔符将它们连接在一起,那么你可以在阅读后进行 df.columns = df.columns.map('_'.join)