Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Excel **Table** with pandas.to_excel()?

Need the achieve this programmatically from a dataframe: enter image description here

https://docs.microsoft.com/en-us/power-bi/service-admin-troubleshoot-excel-workbook-data

like image 209
simpatico Avatar asked Oct 10 '19 15:10

simpatico


People also ask

How convert pandas data frame to multiple Excel sheets?

To write to multiple sheets it is necessary to create an ExcelWriter object with a target file name, and specify a sheet in the file to write to. Multiple sheets may be written to by specifying unique sheet_name . With all data written to the file it is necessary to save the changes.

How do I export data from Jupyter to excel?

Usage. In Jupyter Notebook, just select the 'Excel Spreadsheet (. xlsx)' option under 'Download As' in the File menu. This should output ExcelTest.

Can we use pandas for Excel?

Tasks such as data cleaning, data normalization, visualization, and statistical analysis can be performed on both Excel and Pandas.


2 Answers

I created a package to write properly formatted excel tables from pandas: pandas-xlsx-tables

from pandas_xlsx_tables import df_to_xlsx_table
import pandas as pd

data = [10, 20, 30, 40, 50, 60, 70, 80]
df = pd.DataFrame({'Rank': data,
                'Country': data,
                'Population': data,
                'Strings': [f"n{n}" for n in data],
                'Datetimes': [pd.Timestamp.now() for _ in range(len(data))]})

df_to_xlsx_table(df, "my_table", index=False, header_orientation="diagonal")

You can also do the reverse with xlsx_table_to_df

Excel screenshot

like image 73
Thijs D Avatar answered Sep 19 '22 21:09

Thijs D


Here is one way to do it using XlsxWriter:


import pandas as pd

# Create a Pandas dataframe from some data.
data = [10, 20, 30, 40, 50, 60, 70, 80]
df = pd.DataFrame({'Rank': data,
                   'Country': data,
                   'Population': data,
                   'Data1': data,
                   'Data2': data})

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

# Convert the dataframe to an XlsxWriter Excel object. Turn off the default
# header and index and skip one row to allow us to insert a user defined
# header.
df.to_excel(writer, sheet_name='Sheet1', startrow=1, header=False, index=False)

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

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

# Create a list of column headers, to use in add_table().
column_settings = []
for header in df.columns:
    column_settings.append({'header': header})

# Add the table.
worksheet.add_table(0, 0, max_row, max_col - 1, {'columns': column_settings})

# Make the columns wider for clarity.
worksheet.set_column(0, max_col - 1, 12)

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

Output:

enter image description here

Update: I've added a similar example to the XlsxWriter docs: Example: Pandas Excel output with a worksheet table

like image 21
jmcnamara Avatar answered Sep 20 '22 21:09

jmcnamara