Solución:
El truco / solución alternativa es la única opción (como se ve en @jmcnamara). En resumen es:
import pandas as pd
import xlsxwriter as xw
# random dataframe
d = {'one' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']), 'two' : pd.Series([5., 6., 7., 8.], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
# write data to file
writer = pd.ExcelWriter("test.xlsx", engine="xlsxwriter")
df.to_excel(writer,"sheet with table and header")
# get sheets to add the tables
workbook = writer.book
worksheet_table_header = writer.sheets['sheet with table and header']
# the range in which the table is
end_row = len(df.index)
end_column = len(df.columns)
cell_range = xw.utility.xl_range(0, 0, end_row, end_column)
######################################
# The hack
# Using the index in the Table
df.reset_index(inplace=True)
header = [{'header': di} for di in df.columns.tolist()]
worksheet_table_header.add_table(cell_range,{'header_row': True,'first_column': True,'columns':header})
writer.save()
¿Qué tal esto (tenga en cuenta que las ‘opciones’ solo son necesarias si el marco de datos contiene NA):
import pandas as pd
import xlsxwriter
# random dataframe
d = {'one':pd.Series([1., 2., 3.]), 'two':pd.Series([5., 6., 7., 8.])}
df = pd.DataFrame(d)
workbook = xlsxwriter.Workbook('test.xlsx', options={'nan_inf_to_errors': True})
worksheet = workbook.add_worksheet('sheet1')
worksheet.add_table(0, 0, df.shape[0], df.shape[1]-1,
{'data': df.values.tolist(),
'columns': [{'header': c} for c in df.columns.tolist()],
'style': 'Table Style Medium 9'})
workbook.close()
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)