如何将 Pandas DataFrames 写入多个 Excel 工作表?
在本文中,我们将了解如何使用Python将不同的 DataFrame 导出到不同的 Excel 工作表。
为此, Pandas提供了一个名为xlsxwriter的函数。 ExcelWriter() 是一个允许您将 DataFrame 对象写入 Microsoft Excel 工作表的类。文本、数字、字符串和公式都可以使用 ExcelWriter() 编写。它也可以用于多个工作表。
Syntax:
pandas.ExcelWriter(path, date_format=None, mode=’w’)
Parameter:
- path: (str) Path to xls or xlsx or ods file.
- date_format: Format string for dates written into Excel files (e.g. ‘YYYY-MM-DD’). str, default None
- mode: {‘w’, ‘a’}, default ‘w’. File mode to use (write or append). Append does not work with fsspec URLs.
to_excel() 方法用于将 DataFrame 导出到 excel 文件。要将单个对象写入 excel 文件,我们必须指定目标文件名。如果我们要写入多个工作表,我们需要创建一个具有目标文件名的 ExcelWriter 对象,还需要在我们必须写入的文件中指定工作表。也可以通过指定唯一的 sheet_name 来写入多个工作表。有必要保存写入文件的所有数据的更改。
Syntax:
DataFrame.to_excel(excel_writer, sheet_name=’Sheet1′,index=True)
Parameter:
- excel_writer: path-like, file-like, or ExcelWriter object (new or existing)
- sheet_name: (str, default ‘Sheet1’). Name of the sheet which will contain DataFrame.
- index: (bool, default True). Write row names (index).
使用pandas.DataFrame函数创建一些示例数据框。现在,在 pandas excelwriter函数中创建一个writer 变量并指定您希望存储 excel 文件的路径和文件名。
示例:将 Pandas 数据框写入多个 Excel 工作表
Python3
# import the python pandas package
import pandas as pd
# create data_frame1 by creating a dictionary
# in which values are stored as list
data_frame1 = pd.DataFrame({'Fruits': ['Appple', 'Banana', 'Mango',
'Dragon Fruit', 'Musk melon', 'grapes'],
'Sales in kg': [20, 30, 15, 10, 50, 40]})
# create data_frame2 by creating a dictionary
# in which values are stored as list
data_frame2 = pd.DataFrame({'Vegetables': ['tomato', 'Onion', 'ladies finger',
'beans', 'bedroot', 'carrot'],
'Sales in kg': [200, 310, 115, 110, 55, 45]})
# create data_frame3 by creating a dictionary
# in which values are stored as list
data_frame3 = pd.DataFrame({'Baked Items': ['Cakes', 'biscuits', 'muffins',
'Rusk', 'puffs', 'cupcakes'],
'Sales in kg': [120, 130, 159, 310, 150, 140]})
print(data_frame1)
print(data_frame2)
print(data_frame3)
# create a excel writer object
with pd.ExcelWriter("path to file\filename.xlsx") as writer:
# use to_excel function and specify the sheet_name and index
# to store the dataframe in specified sheet
data_frame1.to_excel(writer, sheet_name="Fruits", index=False)
data_frame2.to_excel(writer, sheet_name="Vegetables", index=False)
data_frame3.to_excel(writer, sheet_name="Baked Items", index=False)
Python3
# import the python pandas package
import pandas as pd
# create data_frame1 by creating a dictionary
# in which values are stored as list
data_frame1 = pd.DataFrame({'Fruits': ['Appple', 'Banana', 'Mango',
'Dragon Fruit', 'Musk melon', 'grapes'],
'Sales in kg': [20, 30, 15, 10, 50, 40]})
# create data_frame2 by creating a dictionary
# in which values are stored as list
data_frame2 = pd.DataFrame({'Vegetables': ['tomato', 'Onion', 'ladies finger',
'beans', 'bedroot', 'carrot'],
'Sales in kg': [200, 310, 115, 110, 55, 45]})
# create data_frame3 by creating a dictionary
# in which values are stored as list
data_frame3 = pd.DataFrame({'Baked Items': ['Cakes', 'biscuits', 'muffins',
'Rusk', 'puffs', 'cupcakes'],
'Sales in kg': [120, 130, 159, 310, 150, 140]})
# create data_frame3 by creating a dictionary
# in which values are stored as list
data_frame4 = pd.DataFrame({'Cool drinks': ['Pepsi', 'Coca-cola', 'Fanta',
'Miranda', '7up', 'Sprite'],
'Sales in count': [1209, 1230, 1359, 3310, 2150, 1402]})
# create a excel writer object as shown using
# Excelwriter function
with pd.ExcelWriter("path_to_file.xlsx", mode="a", engine="openpyxl") as writer:
# use to_excel function and specify the sheet_name and index to
# store the dataframe in specified sheet
data_frame4.to_excel(writer, sheet_name="Cool drinks")
Python3
# import zipfile package
import zipfile
# import the python pandas package
import pandas as pd
# create data_frame1 by creating a dictionary
# in which values are stored as list
data_frame1 = pd.DataFrame({'Fruits': ['Appple', 'Banana', 'Mango',
'Dragon Fruit', 'Musk melon', 'grapes'],
'Sales in kg': [20, 30, 15, 10, 50, 40]})
# create data_frame2 by creating a dictionary
# in which values are stored as list
data_frame2 = pd.DataFrame({'Vegetables': ['tomato', 'Onion', 'ladies finger',
'beans', 'bedroot', 'carrot'],
'Sales in kg': [200, 310, 115, 110, 55, 45]})
# create data_frame3 by creating a dictionary
# in which values are stored as list
data_frame3 = pd.DataFrame({'Baked Items': ['Cakes', 'biscuits', 'muffins',
'Rusk', 'puffs', 'cupcakes'],
'Sales in kg': [120, 130, 159, 310, 150, 140]})
# create data_frame3 by creating a dictionary
# in which values are stored as list
data_frame4 = pd.DataFrame({'Cool drinks': ['Pepsi', 'Coca-cola', 'Fanta',
'Miranda', '7up', 'Sprite'],
'Sales in count': [1209, 1230, 1359, 3310, 2150, 1402]})
# specify the path in which the zip file has to be stored
with zipfile.ZipFile("path_to_file.zip", "w") as zf:
# in open function specify the name in which
# the excel file has to be stored
with zf.open("filename.xlsx", "w") as buffer:
with pd.ExcelWriter(buffer) as writer:
# use to_excel function and specify the sheet_name and
# index to store the dataframe in specified sheet
data_frame1.to_excel(writer, sheet_name="Fruits", index=False)
data_frame2.to_excel(writer, sheet_name="Vegetables", index=False)
data_frame3.to_excel(writer, sheet_name="Baked Items", index=False)
data_frame4.to_excel(writer, sheet_name="Cool Drinks", index=False)
输出:
显示具有不同工作表的 excel 文件的输出保存在指定位置。
示例 2:另一种使用excelwriter将数据框存储在现有 excel 文件中的方法如下所示,
创建数据框并将它们附加到上面显示的现有 excel 文件中,使用 excelwriter函数中的mode= 'a' (意思是append )。使用模式“a”会将新工作表添加为现有 Excel 文件中的最后一个工作表。
Python3
# import the python pandas package
import pandas as pd
# create data_frame1 by creating a dictionary
# in which values are stored as list
data_frame1 = pd.DataFrame({'Fruits': ['Appple', 'Banana', 'Mango',
'Dragon Fruit', 'Musk melon', 'grapes'],
'Sales in kg': [20, 30, 15, 10, 50, 40]})
# create data_frame2 by creating a dictionary
# in which values are stored as list
data_frame2 = pd.DataFrame({'Vegetables': ['tomato', 'Onion', 'ladies finger',
'beans', 'bedroot', 'carrot'],
'Sales in kg': [200, 310, 115, 110, 55, 45]})
# create data_frame3 by creating a dictionary
# in which values are stored as list
data_frame3 = pd.DataFrame({'Baked Items': ['Cakes', 'biscuits', 'muffins',
'Rusk', 'puffs', 'cupcakes'],
'Sales in kg': [120, 130, 159, 310, 150, 140]})
# create data_frame3 by creating a dictionary
# in which values are stored as list
data_frame4 = pd.DataFrame({'Cool drinks': ['Pepsi', 'Coca-cola', 'Fanta',
'Miranda', '7up', 'Sprite'],
'Sales in count': [1209, 1230, 1359, 3310, 2150, 1402]})
# create a excel writer object as shown using
# Excelwriter function
with pd.ExcelWriter("path_to_file.xlsx", mode="a", engine="openpyxl") as writer:
# use to_excel function and specify the sheet_name and index to
# store the dataframe in specified sheet
data_frame4.to_excel(writer, sheet_name="Cool drinks")
输出:
以压缩格式将大型 Pandas DataFrame 写入 excel 文件。
如果输出数据框很大,您还可以将 excel 文件存储为压缩文件。让我们保存我们为此示例创建的数据框。作为excel并将其存储为zip文件。 ZIP 文件格式是一种常见的存档和压缩标准。
Syntax:
ZipFile(file, mode=’r’)
Parameter:
- file: the file can be a path to a file (a string), a file-like object, or a path-like object.
- mode: The mode parameter should be ‘r’ to read an existing file, ‘w’ to truncate and write a new file, ‘a’ to append to an existing file, or ‘x’ to exclusively create and write a new file.
导入zipfile包并创建示例数据框。现在,指定 zip 文件必须存储的路径,这将在指定路径中创建一个zip 文件。创建一个必须存储 excel 文件的文件名。使用to_excel()函数并指定工作表名称和索引以将数据框存储在多个工作表中
示例:以 ZIP 格式编写大型数据帧
Python3
# import zipfile package
import zipfile
# import the python pandas package
import pandas as pd
# create data_frame1 by creating a dictionary
# in which values are stored as list
data_frame1 = pd.DataFrame({'Fruits': ['Appple', 'Banana', 'Mango',
'Dragon Fruit', 'Musk melon', 'grapes'],
'Sales in kg': [20, 30, 15, 10, 50, 40]})
# create data_frame2 by creating a dictionary
# in which values are stored as list
data_frame2 = pd.DataFrame({'Vegetables': ['tomato', 'Onion', 'ladies finger',
'beans', 'bedroot', 'carrot'],
'Sales in kg': [200, 310, 115, 110, 55, 45]})
# create data_frame3 by creating a dictionary
# in which values are stored as list
data_frame3 = pd.DataFrame({'Baked Items': ['Cakes', 'biscuits', 'muffins',
'Rusk', 'puffs', 'cupcakes'],
'Sales in kg': [120, 130, 159, 310, 150, 140]})
# create data_frame3 by creating a dictionary
# in which values are stored as list
data_frame4 = pd.DataFrame({'Cool drinks': ['Pepsi', 'Coca-cola', 'Fanta',
'Miranda', '7up', 'Sprite'],
'Sales in count': [1209, 1230, 1359, 3310, 2150, 1402]})
# specify the path in which the zip file has to be stored
with zipfile.ZipFile("path_to_file.zip", "w") as zf:
# in open function specify the name in which
# the excel file has to be stored
with zf.open("filename.xlsx", "w") as buffer:
with pd.ExcelWriter(buffer) as writer:
# use to_excel function and specify the sheet_name and
# index to store the dataframe in specified sheet
data_frame1.to_excel(writer, sheet_name="Fruits", index=False)
data_frame2.to_excel(writer, sheet_name="Vegetables", index=False)
data_frame3.to_excel(writer, sheet_name="Baked Items", index=False)
data_frame4.to_excel(writer, sheet_name="Cool Drinks", index=False)
输出: