问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

如何将Pandas数据框架写入多个Excel表

创作时间:
作者:
@小白创作中心

如何将Pandas数据框架写入多个Excel表

引用
1
来源
1.
https://geek-docs.com/pandas/python-pandas-io/how-to-write-pandas-dataframes-to-multiple-excel-sheets.html

在数据分析和处理中,经常需要将数据导出到Excel文件中以便进一步处理或分享。Pandas库提供了强大的功能,可以将多个DataFrame写入同一个Excel文件的不同工作表中。本文将详细介绍如何使用Pandas实现这一功能。

基本语法和参数说明

Pandas提供了一个名为ExcelWriter的函数,用于将DataFrame对象写入Microsoft Excel表格。ExcelWriter()类允许在多个工作表中写入文本、数字、字符串和公式。

语法:

pandas.ExcelWriter(path, date_format=None, mode='w')

参数:

  • path: (str) xls 或 xlsx 或 ods 文件的路径。
  • date_format: 写入Excel文件的日期格式字符串(例如,"YYYY-MM-DD")。
  • mode: {‘w’, ‘a’}, default ‘w’. 使用的文件模式(写入或追加)。Append不适用fsspec URLs。

to_excel()方法用于导出数据框架到excel文件。要写一个对象到excel文件,我们必须指定目标文件名。如果我们想写到多个工作表,我们需要创建一个带有目标文件名的ExcelWriter对象,并且需要指定文件中的工作表,我们必须在其中写入。也可以通过指定唯一的sheet_name来写入多个工作表。有必要保存所有写入文件的数据的变化。

语法:

DataFrame.to_excel(excel_writer, sheet_name='Sheet1', index=True)

参数:

  • excel_writer: 类似路径,类似文件,或ExcelWriter对象(新的或现有的)。
  • sheet_name: (str, default ‘Sheet1’). 将包含DataFrame的工作表的名称。
  • index:(bool,默认为True)。写入行名(索引)。

示例:将Pandas数据框架写入多个Excel表

首先,使用pandas.DataFrame函数创建一些样本数据框。然后,创建一个writer变量并指定你希望存储excel文件的路径和文件名,在pandas excelwriter函数里面。

# 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)

输出:
输出显示了不同工作表的Excel文件被保存在指定的位置。

示例2:将数据框架追加到现有Excel文件

另一种方法是使用excelwriter将数据框架存储在现有的excel文件中,如下所示。在excelwriter函数中使用mode='a'(意思是append)创建数据框架并将其追加到上面所示的现有excel文件。使用模式'a'将添加新的工作表作为现有Excel文件的最后一个工作表。

# 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文件格式是一种常见的存档和压缩标准。

语法:

ZipFile(file, mode='r')

参数:

  • file: 文件可以是一个文件的路径(一个字符串),一个类文件对象,或一个类路径对象。
  • mode: 模式参数应该是'r',用于读取一个现有文件,'w'用于截断并写入一个新文件,'a'用于追加到一个现有文件,或者'x'用于专门创建并写入一个新文件。

导入zip文件包并创建样本数据框。现在,指定压缩文件的存储路径,这将在指定的路径上创建一个压缩文件。创建一个文件名,将excel文件保存在其中。使用to_excel()函数并指定工作表名称和索引,以便将数据框架存储在多个工作表中。

例子:以ZIP格式写入大型数据帧

# 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文件的输出样本

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号