简体   繁体   English

如何使用 XlsxWriter 将带有分隔线的 Dataframe 导出到 Excel

[英]How to export a Dataframe to Excel with divider lines using XlsxWriter

I would like to export my df to excel using xlswriter but with format depending on the value of specific cells ie i have a df that look like that:我想使用 xlswriter 将我的 df 导出到 excel 但格式取决于特定单元格的值,即我有一个看起来像这样的 df:

Date Data1 data2
01/01/1979 50.0. 10.0
01/01/1979 50.0. 11.0
02/01/1979 50.0. 11.0
02/01/1979 50.0. 11.0
02/01/1979 50.0. 11.0
03/01/1979 50.0. 11.0
03/01/1979 50.0. 11.0

From a df like that I would like to have an excel file that look like that:从这样的 df 我想要一个看起来像这样的 excel 文件:

Date Data1 data2
01/01/1979 50.0. 10.0
01/01/1979 50.0. 11.0
————————————————————-
02/01/1979 50.0. 11.0
02/01/1979 50.0. 11.0
02/01/1979 50.0. 11.0
————————————————————-
03/01/1979 50.0. 11.0
03/01/1979 50.0. 11.0
————————————————————-

That adds a line to all rows when the date change.当日期更改时,这会在所有行中添加一行。

Below script adds line to each row when Date is changed:Date更改时,下面的脚本会在每一行中添加一行:

Please change delimiter variable to whatever delimiter you desire.请将delimiter变量更改为您想要的任何分隔符。

delimiter = '-----'

def f(x):
    return x.append(pd.DataFrame(delimiter, columns=df.columns, index=[('')]))

df_updated = df.groupby('Date', sort=False, group_keys=False).apply(f)

Output: Output:

    Date        Data1   data2
0   1979-01-01  50.0.   10
1   1979-01-01  50.0.   11
    -----       -----   -----
2   1979-01-02  50.0.   11
3   1979-01-02  50.0.   11
4   1979-01-02  50.0.   11
    -----       -----   -----
5   1979-01-03  50.0.   11
6   1979-01-03  50.0.   11
    -----       -----   -----

One way to do it would be to use a conditional format in Excel via XlsxWriter.一种方法是通过 XlsxWriter 在 Excel 中使用条件格式。 Something like this:像这样的东西:

import pandas as pd


# Create a Pandas dataframe from some data.
df = pd.DataFrame({'Date': ['01/01/1979', '01/01/1979', '02/01/1979',
                            '02/01/1979', '02/01/1979', '03/01/1979',
                            '03/01/1979'],
                   'Data1': [50.0] * 7,
                   'Data2': [11.0] * 7})

df = df[['Date', 'Data1', 'Data2']]

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_conditional.xlsx', engine='xlsxwriter')

# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1', index=False)

# Get the xlsxwriter workbook and worksheet objects.
workbook  = writer.book
worksheet = writer.sheets['Sheet1']

# Make column A wider for clarity.
worksheet.set_column(0, 0, 12)

# Get the dimensions of the dataframe.
(max_row, max_col) = df.shape

# Add a format to use in the conditional format.
format1 = workbook.add_format({'bottom': 1})

# Apply a conditional format to the required cell range.
worksheet.conditional_format(1, 0, max_row, max_col -1,
                             {'type':     'formula',
                              'criteria': '=$A2<>$A3',
                              'format':   format1})


# Close the Pandas Excel writer and output the Excel file.
writer.save()

Output: Output:

在此处输入图像描述

Note, you may want to convert the data strings to actual DateTime objects.请注意,您可能希望将数据字符串转换为实际的 DateTime 对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM