51_Pandas (to_excel) Write Excel files (xlsx, xls)

51_Pandas (to_excel) Write Excel files (xlsx, xls)

Use the to_excel() method to write (save) a pandas.DataFrame as an Excel file (extension: .xlsx, .xls).

Here, the following will be described.

  • Install openpyxl, xlwt
  • Write DataFrame to Excel file (create/overwrite save)
  • Write multiple DataFrames to Excel file (create/overwrite save)
  • Write DataFrame to existing Excel file (append)

For reading Excel files, refer to the following articles

In addition, see the following article for reading and writing csv files and json files with pandas.

Install openpyxl, xlwt

to_excel() internally uses openpyxl and xlwt libraries. openpyxl is for writing .xlsx (the format since Excel 2007), and xlwt is for writing .xls files (as of Excel 2003).

Both can be installed with pip. (environment pip3)

$ pip install openpyxl
$ pip install xlwt

Write DataFrame to Excel file (create/overwrite save)

Take the pandas.DataFrame below as an example.

import pandas as pd

print(pd.__version__)
# 1.2.2

df = pd.DataFrame([[11, 21, 31], [12, 22, 32], [31, 32, 33]],
                  index=['one', 'two', 'three'], columns=['a', 'b', 'c'])

print(df)
#         a   b   c
# one    11  21  31
# two    12  22  32
# three  31  32  33

If a path is specified as the first argument to the to_excel() method, a new file will be created if it does not exist, or overwritten if it exists. Note that the data in the original file will be deleted when overwriting. An example of adding a new sheet to an existing file will be described later. The sheet name is specified for the parameter sheet_name. If omitted, the name will be Sheet1.

df.to_excel('data/dst/pandas_to_excel.xlsx', sheet_name='new_sheet_name')

If you don't need to write index (row names) and columns (column names), set parameter index and title to False.

df.to_excel('data/dst/pandas_to_excel_no_index_header.xlsx',
            index=False, header=False)

Write multiple DataFrames to Excel file (create/overwrite save)

Multiple pandas.DataFrame objects can be written to separate worksheets using the ExcelWriter object.
For example, prepare another pandas.DataFrame object.

df2 = df[['a', 'c']]
print(df2)
#         a   c
# one    11  31
# two    12  32
# three  31  33

Specify the path to pandas.ExcelWriter() to generate an ExcelWriter object and specify it as the first argument to the to_excel() method. Using a with block is easier because you don't need to call writer.save() and writer.close().

with pd.ExcelWriter('data/dst/pandas_to_excel_multi.xlsx') as writer:
    df.to_excel(writer, sheet_name='sheet1')
    df2.to_excel(writer, sheet_name='sheet2')

Write DataFrame to existing Excel file (append)

If you set the parameter mode='a' in pandas.ExcelWriter(), it will be in append mode and you can add a pandas.DataFrame as a new worksheet to an existing Excel file.

with pd.ExcelWriter('data/dst/pandas_to_excel.xlsx', mode='a') as writer:
    df.to_excel(writer, sheet_name='new_sheet1')
    df2.to_excel(writer, sheet_name='new_sheet2')

If you specify an existing worksheet name, a new worksheet is added by adding a number starting with 1 to the end of the worksheet name. Existing worksheets are not overwritten.

If you want to edit/add (change cell values, add new cells, etc.), while maintaining the format of the existing Excel file table instead of adding a new table, it is convenient to operate directly with openpyxl.

Guess you like

Origin blog.csdn.net/qq_18351157/article/details/126483158