Remove default formatting in header when converting pandas DataFrame to excel sheet
Asked Answered
D

3

17

This is something that has been answered and re-answered time and time again because the answer keeps changing with updates to pandas. I tried some of the solutions I found here and elsewhere online and none of them have worked for me on the current version of pandas. Does anyone know the current, March 2019, pandas 0.24.2, fix for removing the default styling that a DataFrame gives to its header when converting it to an excel sheet? Simply using xlsxwriter to overwrite the styling does not work because of an issue with precedence.

Drawshave answered 19/3, 2019 at 16:50 Comment(2)
Could you add links of the previous questions/answers that no longer work?Sailing
For example, this thread was a bust for me #36694813Drawshave
U
9

Based largely on an example provided in the Xlsxwriter Docs (link here), the fully reproducible example below removes the default pandas header format in pandas 0.24.2. Of note is that in df.to_excel(), I'm changing the header and startrow parameters.

import xlsxwriter
import pandas as pd
import numpy as np

# Creating a dataframe 
df = pd.DataFrame(np.random.randn(100, 3), columns=list('ABC'))
column_list = df.columns
# Create a Pandas Excel writer using XlsxWriter engine.
writer = pd.ExcelWriter("test.xlsx", engine='xlsxwriter')

df.to_excel(writer, sheet_name='Sheet1', startrow=1, header=False, index=False)

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


for idx, val in enumerate(column_list):
    worksheet.write(0, idx, val)

writer.save()

print(pd.__version__)

Expected Output:

0.24.2

Expected Output

Ultranationalism answered 19/3, 2019 at 17:27 Comment(1)
Clever solution. So you're essentially just removing the column labels, and then in a for loop, adding them back in, thus bypassing the default header styles. Worked great, thanks!Drawshave
P
23

Consider adjusting the header style property as a global setting:

import pandas as pd

pd.io.formats.excel.ExcelFormatter.header_style = None


...
mydataframe.to_excel(...)

Recent API changes require slight modification to reference the attribute:

import pandas as pd

from pandas.io.formats import excel
excel.ExcelFormatter.header_style = None

...
mydataframe.to_excel(...)
Particularize answered 28/5, 2022 at 23:8 Comment(0)
U
9

Based largely on an example provided in the Xlsxwriter Docs (link here), the fully reproducible example below removes the default pandas header format in pandas 0.24.2. Of note is that in df.to_excel(), I'm changing the header and startrow parameters.

import xlsxwriter
import pandas as pd
import numpy as np

# Creating a dataframe 
df = pd.DataFrame(np.random.randn(100, 3), columns=list('ABC'))
column_list = df.columns
# Create a Pandas Excel writer using XlsxWriter engine.
writer = pd.ExcelWriter("test.xlsx", engine='xlsxwriter')

df.to_excel(writer, sheet_name='Sheet1', startrow=1, header=False, index=False)

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


for idx, val in enumerate(column_list):
    worksheet.write(0, idx, val)

writer.save()

print(pd.__version__)

Expected Output:

0.24.2

Expected Output

Ultranationalism answered 19/3, 2019 at 17:27 Comment(1)
Clever solution. So you're essentially just removing the column labels, and then in a for loop, adding them back in, thus bypassing the default header styles. Worked great, thanks!Drawshave
P
1

The key explanation is that: pandas writes a df's header with set_cell(). A cell format (in xlsxwriter speak, a "format" is a FormatObject that you have to add to the worksheetObject) can NOT be overridden with set_row(). If you are using set_row() to your header row, it will not work, you have to use set_cell().

Pesthole answered 13/4, 2019 at 4:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.