合并Excel工作表中某些列的公用值

ma8fv8wu  于 2023-01-27  发布在  其他
关注(0)|答案(1)|浏览(131)

考虑 Dataframe df

df = pd.DataFrame({'Name': ['Tesla','Tesla','Tesla','Toyota','Ford','Ford','Ford','BMW','BMW','BMW','Mercedes','Mercedes','Mercedes'],
                   'Type': ['Model X','Model X','Model X','Corolla','Bronco','Bronco','Mustang','3 Series','3 Series','7 Series','C-Class','C-Class','S-Class'],
                   'Year': [2015, 2015, 2015, 2017, 2018, 2018, 2020, 2015, 2015, 2017, 2018, 2018, 2020],
                   'Price': [85000, 90000, 95000, 20000, 35000, 35000, 45000, 40000, 40000, 65000, 50000, 50000, 75000],
                   'Color': ['White','White','White','Red','Blue','Blue','Yellow','Silver','Silver','Black','White','White','Black']
                  })

我试图合并Excel中的单元格,其中有一个DataFrame df列连续使用下面的mergecells函数的共同值,但是,当我打开合并后的Excel文件,它说Excel文件已恢复一些值。
x一个一个一个一个x一个一个二个x
但是当我用上面的代码调用上面的merge函数时,我得到的错误如下图所示

Type列、Name列和Price列正确合并,但年份和颜色完全错误
预期输出

s3fp2yjn

s3fp2yjn1#

问题出在groupby上,当按颜色或年份分组时,间隔不相交:行[1, 2, 3][11, 12]中都有白色。您应该考虑列中的连续值。more_itertools.consecutive_groups可以帮助您做到这一点:

from more_itertools import consecutive_groups

sheetname='Sheet1'

with pd.ExcelWriter("test.xlsx") as writer:
    df.to_excel(writer, sheet_name=sheetname, index=False)
    wb = writer.book
    ws = writer.sheets[sheetname]
    mf = wb.add_format({'align': 'center', 'valign': 'vcenter'})

    for j, col in enumerate(df.columns):
        ws.set_column(j, j, 12, mf)
        for val in df[col].unique():
            idx = df[(df[col]==val) & (df[col]==df[col].shift(1))].index   # indices of the rows where the value is the same as the previous row
            for seg in consecutive_groups(idx):
                l = list(seg)
                ws.merge_range(l[0], j, l[-1]+1, j, val, mf)

相关问题