To excel different sheets

Improve Article

Save Article

Like Article

  • Read
  • Discuss
  • Improve Article

    Save Article

    Like Article

    In this article, we will see how to export different DataFrames to different excel sheets using python.

    Pandas provide a function called xlsxwriter for this purpose. ExcelWriter() is a class that allows you to write DataFrame objects into Microsoft Excel sheets. Text, numbers, strings, and formulas can all be written using ExcelWriter(). It can also be used on several worksheets.

    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.

    The to_excel() method is used to export the DataFrame to the excel file. To write a single object to the excel file, we have to specify the target file name. If we want to write to multiple sheets, we need to create an ExcelWriter object with target filename and also need to specify the sheet in the file in which we have to write. The multiple sheets can also be written by specifying the unique sheet_name. It is necessary to save the changes for all the data written to the file.

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

    Create some sample data frames using pandas.DataFrame function. Now, create a writer variable and specify the path in which you wish to store the excel file and the file name, inside the pandas excelwriter function.

    Example: Write Pandas dataframe to multiple excel sheets

    Python3

    import pandas as pd

    data_frame1 = pd.DataFrame({'Fruits': ['Appple', 'Banana', 'Mango',

                                           'Dragon Fruit', 'Musk melon', 'grapes'],

                                'Sales in kg': [20, 30, 15, 10, 50, 40]})

    data_frame2 = pd.DataFrame({'Vegetables': ['tomato', 'Onion', 'ladies finger',

                                               'beans', 'bedroot', 'carrot'],

                                'Sales in kg': [200, 310, 115, 110, 55, 45]})

    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)

    with pd.ExcelWriter("path to filefilename.xlsx") as writer:

        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)

    Output:

    The output showing the excel file with different sheets got saved in the specified location.

    Example 2: Another method to store the dataframe in an existing excel file using excelwriter is shown below,

    Create dataframe(s) and Append them to the existing excel file shown above using mode= ‘a’ (meaning append) in the excelwriter function. Using mode ‘a’ will add the new sheet as the last sheet in the existing excel file.

    Python3

    import pandas as pd

    data_frame1 = pd.DataFrame({'Fruits': ['Appple', 'Banana', 'Mango',

                                           'Dragon Fruit', 'Musk melon', 'grapes'],

                                'Sales in kg': [20, 30, 15, 10, 50, 40]})

    data_frame2 = pd.DataFrame({'Vegetables': ['tomato', 'Onion', 'ladies finger',

                                               'beans', 'bedroot', 'carrot'],

                                'Sales in kg': [200, 310, 115, 110, 55, 45]})

    data_frame3 = pd.DataFrame({'Baked Items': ['Cakes', 'biscuits', 'muffins',

                                                'Rusk', 'puffs', 'cupcakes'],

                                'Sales in kg': [120, 130, 159, 310, 150, 140]})

    data_frame4 = pd.DataFrame({'Cool drinks': ['Pepsi', 'Coca-cola', 'Fanta',

                                                'Miranda', '7up', 'Sprite'],

                                'Sales in count': [1209, 1230, 1359, 3310, 2150, 1402]})

    with pd.ExcelWriter("path_to_file.xlsx", mode="a", engine="openpyxl") as writer:

        data_frame4.to_excel(writer, sheet_name="Cool drinks")

    Output:

    Writing Large Pandas DataFrame to excel file in a zipped format.

    If the output dataframe is large, you can also store the excel file as a zipped file. Let’s save the dataframe which we created for this example. as excel and store it as a zip file. The ZIP file format is a common archive and compression standard.

    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.

    Import the zipfile package and create sample dataframes. Now, specify the path in which the zip file has to be stored, This creates a zip file in the specified path. Create a file name in which the excel file has to be stored. Use to_excel() function and specify the sheet name and index to store the dataframe in multiple sheets

    Example: Write large dataframes in ZIP format

    Python3

    import zipfile

    import pandas as pd

    data_frame1 = pd.DataFrame({'Fruits': ['Appple', 'Banana', 'Mango',

                                           'Dragon Fruit', 'Musk melon', 'grapes'],

                                'Sales in kg': [20, 30, 15, 10, 50, 40]})

    data_frame2 = pd.DataFrame({'Vegetables': ['tomato', 'Onion', 'ladies finger',

                                               'beans', 'bedroot', 'carrot'],

                                'Sales in kg': [200, 310, 115, 110, 55, 45]})

    data_frame3 = pd.DataFrame({'Baked Items': ['Cakes', 'biscuits', 'muffins',

                                                'Rusk', 'puffs', 'cupcakes'],

                                'Sales in kg': [120, 130, 159, 310, 150, 140]})

    data_frame4 = pd.DataFrame({'Cool drinks': ['Pepsi', 'Coca-cola', 'Fanta',

                                                'Miranda', '7up', 'Sprite'],

                                'Sales in count': [1209, 1230, 1359, 3310, 2150, 1402]})

    with zipfile.ZipFile("path_to_file.zip", "w") as zf:

        with zf.open("filename.xlsx", "w") as buffer:

            with pd.ExcelWriter(buffer) as writer:

                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)

     Output:

    Sample output of zipped excel file

    Like Article

    Save Article

    can you teach me whether Python can write into a same Excel file, but 2 different spreadsheets (tabs)?

    Just for example, I want to pick and write the titles of below 4 websites, and write them into the same file title.xls but respectively in its Sheet1 and Sheet 2.

    www.dailynews.com
    www.dailynews.co.zw
    www.gulf-daily-news.com
    www.dailynews.gov.bw
    

    I do them in 2 scripts, each for 2 websites:

    from bs4 import BeautifulSoup
    import urllib2
    import xlwt
    
    line_in_list = ['www.dailynews.com','www.dailynews.co.zw'] 
    # line_in_list = [www.gulf-daily-news.com','www.dailynews.gov.bw']
    
    book = xlwt.Workbook(encoding='utf-8', style_compression = 0)
    sheet = book.add_sheet('Sheet1', cell_overwrite_ok = True) 
    # sheet = book.add_sheet('Sheet2', cell_overwrite_ok = True)
    
    for cor,websites in enumerate(line_in_list):
        url = "http://" + websites
        page = urllib2.urlopen(url)
        soup = BeautifulSoup(page.read())
        site_title = soup.find_all("title")
        print site_title
        sheet.write (cor, 0, site_title[0].text)
    
    book.save("title.xls")
    

    however, the script is overwriting the sheets. I can only have either Sheet1 or Sheet2 but never both.

    any helps? thanks.

    In this tutorial, you’ll learn how to save your Pandas DataFrame or DataFrames to Excel files. Being able to save data to this ubiquitous data format is an important skill in many organizations. In this tutorial, you’ll learn how to save a simple DataFrame to Excel, but also how to customize your options to create the report you want!

    By the end of this tutorial, you’ll have learned:

    • How to save a Pandas DataFrame to Excel
    • How to customize the sheet name of your DataFrame in Excel
    • How to customize the index and column names when writing to Excel
    • How to write multiple DataFrames to Excel in Pandas
    • Whether to merge cells or freeze panes when writing to Excel in Pandas
    • How to format missing values and infinity values when writing Pandas to Excel

    Let’s get started!

    The Quick Answer: Use Pandas to_excel

    To write a Pandas DataFrame to an Excel file, you can apply the .to_excel() method to the DataFrame, as shown below:

    # Saving a Pandas DataFrame to an Excel File
    # Without a Sheet Name
    df.to_excel(file_name)
    
    # With a Sheet Name
    df.to_excel(file_name, sheet_name='My Sheet')
    
    # Without an Index
    df.to_excel(file_name, index=False)

    Understanding the Pandas to_excel Function

    Before diving into any specifics, let’s take a look at the different parameters that the method offers. The method provides a ton of different options, allowing you to customize the output of your DataFrame in many different ways. Let’s take a look:

    # The many parameters of the .to_excel() function
    df.to_excel(excel_writer, sheet_name='Sheet1', na_rep='', float_format=None, columns=None, header=True, index=True, index_label=None, startrow=0, startcol=0, engine=None, merge_cells=True, encoding=None, inf_rep='inf', verbose=True, freeze_panes=None, storage_options=None)

    Let’s break down what each of these parameters does:

    Parameter Description Available Options
    excel_writer= The path of the ExcelWriter to use path-like, file-like, or ExcelWriter object
    sheet_name= The name of the sheet to use String representing name, default ‘Sheet1’
    na_rep= How to represent missing data String, default ''
    float_format= Allows you to pass in a format string to format floating point values String
    columns= The columns to use when writing to the file List of strings. If blank, all will be written
    header= Accepts either a boolean or a list of values. If a boolean, will either include the header or not. If a list of values is provided, aliases will be used for the column names. Boolean or list of values
    index= Whether to include an index column or not. Boolean
    index_label= Column labels to use for the index. String or list of strings.
    startrow= The upper left cell to start the DataFrame on. Integer, default 0
    startcol= The upper left column to start the DataFrame on Integer, default 0
    engine= The engine to use to write. openpyxl or xlsxwriter
    merge_cells= Whether to write multi-index cells or hierarchical rows as merged cells Boolean, default True
    encoding= The encoding of the resulting file. String
    inf_rep= How to represent infinity values (as Excel doesn’t have a representation) String, default 'inf'
    verbose= Whether to display more information in the error logs. Boolean, default True
    freeze_panes= Allows you to pass in a tuple of the row, column to start freezing panes on Tuple of integers with length 2
    storage_options= Extra options that allow you to save to a particular storage connection Dictionary
    The many parameters of the Pandas .to_excel() method

    How to Save a Pandas DataFrame to Excel

    The easiest way to save a Pandas DataFrame to an Excel file is by passing a path to the .to_excel() method. This will save the DataFrame to an Excel file at that path, overwriting an Excel file if it exists already.

    Let’s take a look at how this works:

    # Saving a Pandas DataFrame to an Excel File
    import pandas as pd
    df = pd.DataFrame.from_dict(
        {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
    )
    
    df.to_excel('filename.xlsx')

    Running the code as shown above will save the file with all other default parameters. This returns the following image:

    01 - Saving Pandas DataFrame to Excel with Index

    You can specify a sheetname by using the sheet_name= parameter. By default, Pandas will use 'sheet1'.

    # Specifying a Sheet Name When Saving to Excel
    import pandas as pd
    df = pd.DataFrame.from_dict(
        {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
    )
    
    df.to_excel('filename.xlsx', sheet_name='Your Sheet')

    This returns the following workbook:

    02 - Saving Pandas DataFrame to Excel with Sheet Name

    In the following section, you’ll learn how to customize whether to include an index column or not.

    How to Include an Index when Saving a Pandas DataFrame to Excel

    By default, Pandas will include the index when saving a Pandas Dataframe to an Excel file. This can be helpful when the index is a meaningful index (such as a date and time). However, in many cases, the index will simply represent the values from 0 through to the end of the records.

    If you don’t want to include the index in your Excel file, you can use the index= parameter, as shown below:

    # How to exclude the index when saving a DataFrame to Excel
    import pandas as pd
    df = pd.DataFrame.from_dict(
        {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
    )
    
    df.to_excel('filename.xlsx', index=False)

    This returns the following Excel file:

    03- Saving Pandas DataFrame to Excel without Index

    In the following section, you’ll learn how to rename an index when saving a Pandas DataFrame to an Excel file.

    How to Rename an Index when Saving a Pandas DataFrame to Excel

    By default, Pandas will not named the index of your DataFrame. This, however, can be confusing and can lead to poorer results when trying to manipulate the data in Excel, either by filtering or by pivoting the data. Because of this, it can be helpful to provide a name or names for your indices.

    Pandas makes this easy by using the index_label= parameter. This parameter accepts either a single string (for a single index) or a list of strings (for a multi-index). Check out below how you can use this parameter:

    # Providing a name for your Pandas index
    import pandas as pd
    df = pd.DataFrame.from_dict(
        {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
    )
    
    df.to_excel('filename.xlsx', index_label='Your Index')

    This returns the following sheet:

    04 - Saving Pandas DataFrame to Excel with Labelled Index

    How to Save Multiple DataFrames to Different Sheets in Excel

    One of the tasks you may encounter quite frequently is the need to save multi Pandas DataFrames to the same Excel file, but in different sheets. This is where Pandas makes it a less intuitive. If you were to simply write the following code, the second command would overwrite the first command:

    # The wrong way to save multiple DataFrames to the same workbook
    import pandas as pd
    df = pd.DataFrame.from_dict(
        {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
    )
    
    df.to_excel('filename.xlsx', sheet_name='Sheet1')
    df.to_excel('filename.xlsx', sheet_name='Sheet2')

    Instead, we need to use a Pandas Excel Writer to manage opening and saving our workbook. This can be done easily by using a context manager, as shown below:

    # The Correct Way to Save Multiple DataFrames to the Same Workbook
    import pandas as pd
    df = pd.DataFrame.from_dict(
        {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
    )
    
    with pd.ExcelWriter('filename.xlsx') as writer:
        df.to_excel(writer, sheet_name='Sheet1')
        df.to_excel(writer, sheet_name='Sheet2')

    This will create multiple sheets in the same workbook. The sheets will be created in the same order as you specify them in the command above.

    This returns the following workbook:

    05 - Saving Multiple Pandas Dataframes to Excel Worksheets

    How to Save Only Some Columns when Exporting Pandas DataFrames to Excel

    When saving a Pandas DataFrame to an Excel file, you may not always want to save every single column. In many cases, the Excel file will be used for reporting and it may be redundant to save every column. Because of this, you can use the columns= parameter to accomplish this.

    Let’s see how we can save only a number of columns from our dataset:

    # Saving Only a Subset of Columns to Excel
    import pandas as pd
    df = pd.DataFrame.from_dict(
        {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
    )
    
    df.to_excel('filename.xlsx', columns=['A', 'B'])

    This returns the following Excel file:

    06 - Saving Pandas DataFrame to Excel with only some columns

    How to Rename Columns when Exporting Pandas DataFrames to Excel

    Continuing our discussion about how to handle Pandas DataFrame columns when exporting to Excel, we can also rename our columns in the saved Excel file. The benefit of this is that we can work with aliases in Pandas, which may be easier to write, but then output presentation-ready column names when saving to Excel.

    We can accomplish this using the header= parameter. The parameter accepts either a boolean value of a list of values. If a boolean value is passed, you can decide whether to include or a header or not. When a list of strings is provided, then you can modify the column names in the resulting Excel file, as shown below:

    # Modifying Column Names when Exporting a Pandas DataFrame to Excel
    import pandas as pd
    df = pd.DataFrame.from_dict(
        {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
    )
    
    df.to_excel('filename.xlsx', header=['New_A', 'New_B', 'New_C'])

    This returns the following Excel sheet:

    07 - Saving Pandas DataFrame to Excel with renamed columns.png

    How to Specify Starting Positions when Exporting a Pandas DataFrame to Excel

    One of the interesting features that Pandas provides is the ability to modify the starting position of where your DataFrame will be saved on the Excel sheet. This can be helpful if you know you’ll be including different rows above your data or a logo of your company.

    Let’s see how we can use the startrow= and startcol= parameters to modify this:

    # Changing the Start Row and Column When Saving a DataFrame to an Excel File
    import pandas as pd
    df = pd.DataFrame.from_dict(
        {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
    )
    
    df.to_excel('filename.xlsx', startcol=3, startrow=2)

    This returns the following worksheet:

    08 - Specifying starting position for excel

    How to Represent Missing and Infinity Values When Saving Pandas DataFrame to Excel

    In this section, you’ll learn how to represent missing data and infinity values when saving a Pandas DataFrame to Excel. Because Excel doesn’t have a way to represent infinity, Pandas will default to the string 'inf' to represent any values of infinity.

    In order to modify these behaviors, we can use the na_rep= and inf_rep= parameters to modify the missing and infinity values respectively. Let’s see how we can do this by adding some of these values to our DataFrame:

    # Customizing Output of Missing and Infinity Values When Saving to Excel
    import pandas as pd
    import numpy as np
    
    df = pd.DataFrame.from_dict(
        {'A': [1, np.NaN, 3], 'B': [4, 5, np.inf], 'C': [7, 8, 9]}
    )
    
    df.to_excel('filename.xlsx', na_rep='NA', inf_rep='INFINITY')

    This returns the following worksheet:

    09 - Saving Pandas DataFrame to Excel with infinity and missing data

    How to Merge Cells when Writing Multi-Index DataFrames to Excel

    In this section, you’ll learn how to modify the behavior of multi-index DataFrames when saved to Excel. By default Pandas will set the merge_cells= parameter to True, meaning that the cells will be merged. Let’s see what happens when we set this behavior to False, indicating that the cells should not be merged:

    # Modifying Merge Cell Behavior for Multi-Index DataFrames
    import pandas as pd
    import numpy as np
    from random import choice
    
    df = pd.DataFrame.from_dict({
        'A': np.random.randint(0, 10, size=50),
        'B': [choice(['a', 'b', 'c']) for i in range(50)],
        'C': np.random.randint(0, 3, size=50)})
    
    pivot = df.pivot_table(index=['B', 'C'], values='A')
    
    pivot.to_excel('filename.xlsx', merge_cells=False)

    This returns the Excel worksheet below:

    10 - Prevent merging cells when saving Pandas to Excel

    How to Freeze Panes when Saving a Pandas DataFrame to Excel

    In this final section, you’ll learn how to freeze panes in your resulting Excel worksheet. This allows you to specify the row and column at which you want Excel to freeze the panes. This can be done using the freeze_panes= parameter. The parameter accepts a tuple of integers (of length 2). The tuple represents the bottommost row and the rightmost column that is to be frozen.

    Let’s see how we can use the freeze_panes= parameter to freeze our panes in Excel:

    # Freezing Panes in an Excel Workbook Using Pandas
    import pandas as pd
    import numpy as np
    
    df = pd.DataFrame.from_dict(
        {'A': [1, np.NaN, 3], 'B': [4, 5, np.inf], 'C': [7, 8, 9]}
    )
    
    df.to_excel('filename.xlsx', freeze_panes=(3,4))

    This returns the following workbook:

    11 - Saving Pandas DataFrame to Excel with frozen panes

    Conclusion

    In this tutorial, you learned how to save a Pandas DataFrame to an Excel file using the to_excel method. You first explored all of the different parameters that the function had to offer at a high level. Following that, you learned how to use these parameters to gain control over how the resulting Excel file should be saved. For example, you learned how to specify sheet names, index names, and whether to include the index or not. Then you learned how to include only some columns in the resulting file and how to rename the columns of your DataFrame. You also learned how to modify the starting position of the data and how to freeze panes.

    Additional Resources

    To learn more about related topics, check out the tutorials below:

    • How to Use Pandas to Read Excel Files in Python
    • Pandas Dataframe to CSV File – Export Using .to_csv()
    • Introduction to Pandas for Data Science
    • Official Documentation: Pandas to_excel

    An example of writing multiple dataframes to worksheets using Pandas and
    XlsxWriter.

    ##############################################################################
    #
    # An example of writing multiple dataframes to worksheets using Pandas and
    # XlsxWriter.
    #
    # SPDX-License-Identifier: BSD-2-Clause
    # Copyright 2013-2023, John McNamara, jmcnamara@cpan.org
    #
    
    import pandas as pd
    
    
    # Create some Pandas dataframes from some data.
    df1 = pd.DataFrame({"Data": [11, 12, 13, 14]})
    df2 = pd.DataFrame({"Data": [21, 22, 23, 24]})
    df3 = pd.DataFrame({"Data": [31, 32, 33, 34]})
    
    # Create a Pandas Excel writer using XlsxWriter as the engine.
    writer = pd.ExcelWriter("pandas_multiple.xlsx", engine="xlsxwriter")
    
    # Write each dataframe to a different worksheet.
    df1.to_excel(writer, sheet_name="Sheet1")
    df2.to_excel(writer, sheet_name="Sheet2")
    df3.to_excel(writer, sheet_name="Sheet3")
    
    # Close the Pandas Excel writer and output the Excel file.
    writer.close()
    

    Время чтения 4 мин.

    Python Pandas — это библиотека для анализа данных. Она может читать, фильтровать и переупорядочивать небольшие и большие наборы данных и выводить их в различных форматах, включая Excel. ExcelWriter() определен в библиотеке Pandas.

    Содержание

    1. Что такое функция Pandas.ExcelWriter() в Python?
    2. Синтаксис
    3. Параметры
    4. Возвращаемое значение
    5. Пример программы с Pandas ExcelWriter()
    6. Что такое функция Pandas DataFrame to_excel()?
    7. Запись нескольких DataFrames на несколько листов
    8. Заключение

    Метод Pandas.ExcelWriter() — это класс для записи объектов DataFrame в файлы Excel в Python. ExcelWriter() можно использовать для записи текста, чисел, строк, формул. Он также может работать на нескольких листах.Для данного примера необходимо, чтоб вы установили на свой компьютер библиотеки Numpy и Pandas.

    Синтаксис

    pandas.ExcelWriter(path, engine= None, date_format=None, datetime_format=None, mode=w,**engine_krawgs)

    Параметры

    Все параметры установлены на значения по умолчанию.

    Функция Pandas.ExcelWriter() имеет пять параметров.

    1. path: имеет строковый тип, указывающий путь к файлу xls или xlsx.
    2. engine: он также имеет строковый тип и является необязательным. Это движок для написания.
    3. date_format: также имеет строковый тип и имеет значение по умолчанию None. Он форматирует строку для дат, записанных в файлы Excel.
    4. datetime_format: также имеет строковый тип и имеет значение по умолчанию None. Он форматирует строку для объектов даты и времени, записанных в файлы Excel.
    5. Mode: это режим файла для записи или добавления. Его значение по умолчанию — запись, то есть ‘w’.

    Возвращаемое значение

    Он экспортирует данные в файл Excel.

    Пример программы с Pandas ExcelWriter()

    Вам необходимо установить и импортировать модуль xlsxwriter. Если вы используете блокнот Jupyter, он вам не понадобится; в противном случае вы должны установить его.

    Напишем программу, показывающую работу ExcelWriter() в Python.

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    import pandas as pd

    import numpy as np

    import xlsxwriter

    # Creating dataset using dictionary

    data_set = {

        ‘Name’: [‘Rohit’, ‘Arun’, ‘Sohit’, ‘Arun’, ‘Shubh’],

        ‘Roll no’: [’01’, ’02’, ’03’, ’04’, np.nan],

        ‘maths’: [’93’, ’63’, np.nan, ’94’, ’83’],

        ‘science’: [’88’, np.nan, ’66’, ’94’, np.nan],

        ‘english’: [’93’, ’74’, ’84’, ’92’, ’87’]}

    # Converting into dataframe

    df = pd.DataFrame(data_set)

    # Writing the data into the excel sheet

    writer_obj = pd.ExcelWriter(‘Write.xlsx’,

                                engine=‘xlsxwriter’)

    df.to_excel(writer_obj, sheet_name=‘Sheet’)

    writer_obj.save()

    print(‘Please check out the Write.xlsx file.’)

    Выход:

    Please check out the Write.xlsx file.

    Содержимое файла Excel следующее.

    Метод ExcelWriter()

    В приведенном выше коде мы создали DataFrame, в котором хранятся данные студентов. Затем мы создали объект для записи данных DataFrame на лист Excel, и после записи данных мы сохранили лист. Некоторые значения в приведенном выше листе Excel пусты, потому что в DataFrame эти значения — np.nan. Чтобы проверить данные DataFrame, проверьте лист Excel.

    Что такое функция Pandas DataFrame to_excel()?

    Функция Pandas DataFrame to_excel() записывает объект на лист Excel. Мы использовали функцию to_excel() в приведенном выше примере, потому что метод ExcelWriter() возвращает объект записи, а затем мы используем метод DataFrame.to_excel() для его экспорта в файл Excel.

    Чтобы записать один объект в файл Excel .xlsx, необходимо только указать имя целевого файла. Для записи на несколько листов необходимо создать объект ExcelWriter с именем целевого файла и указать лист в файле для записи.

    На несколько листов можно записать, указав уникальное имя листа. При записи всех данных в файл необходимо сохранить изменения. Обратите внимание, что создание объекта ExcelWriter с уже существующим именем файла приведет к удалению содержимого существующего файла.

    Мы также можем написать приведенный выше пример, используя Python с оператором.

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    import pandas as pd

    import numpy as np

    import xlsxwriter

    # Creating dataset using dictionary

    data_set = {

        ‘Name’: [‘Rohit’, ‘Arun’, ‘Sohit’, ‘Arun’, ‘Shubh’],

        ‘Roll no’: [’01’, ’02’, ’03’, ’04’, np.nan],

        ‘maths’: [’93’, ’63’, np.nan, ’94’, ’83’],

        ‘science’: [’88’, np.nan, ’66’, ’94’, np.nan],

        ‘english’: [’93’, ’74’, ’84’, ’92’, ’87’]}

    # Converting into dataframe

    df = pd.DataFrame(data_set)

    with pd.ExcelWriter(‘WriteWith.xlsx’, engine=‘xlsxwriter’) as writer:

        df.to_excel(writer, sheet_name=‘Sheet’)

    print(‘Please check out the WriteWith.xlsx file.’)

    Выход:

    Please check out the WriteWith.xlsx file.

    Вы можете проверить файл WriteWith.xlsx и просмотреть его содержимое. Это будет то же самое, что и файл Write.xlsx.

    Запись нескольких DataFrames на несколько листов

    В приведенном выше примере мы видели только один лист для одного фрейма данных. Мы можем написать несколько фреймов с несколькими листами, используя Pandas.ExcelWriter.

    Давайте напишем пример, в котором мы создадим три DataFrames и сохраним эти DataFrames в файле multiplesheet.xlsx с тремя разными листами.

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    import pandas as pd

    import numpy as np

    import xlsxwriter

    # Creating dataset using dictionary

    data_set = {

        ‘Name’: [‘Rohit’, ‘Arun’, ‘Sohit’, ‘Arun’, ‘Shubh’],

        ‘Roll no’: [’01’, ’02’, ’03’, ’04’, np.nan],

        ‘maths’: [’93’, ’63’, np.nan, ’94’, ’83’],

        ‘science’: [’88’, np.nan, ’66’, ’94’, np.nan],

        ‘english’: [’93’, ’74’, ’84’, ’92’, ’87’]}

    data_set2 = {

        ‘Name’: [‘Ankit’, ‘Krunal’, ‘Rushabh’, ‘Dhaval’, ‘Nehal’],

        ‘Roll no’: [’01’, ’02’, ’03’, ’04’, np.nan],

        ‘maths’: [’93’, ’63’, np.nan, ’94’, ’83’],

        ‘science’: [’88’, np.nan, ’66’, ’94’, np.nan],

        ‘english’: [’93’, ’74’, ’84’, ’92’, ’87’]}

    data_set3 = {

        ‘Name’: [‘Millie’, ‘Jane’, ‘Michael’, ‘Bobby’, ‘Brown’],

        ‘Roll no’: [’01’, ’02’, ’03’, ’04’, np.nan],

        ‘maths’: [’93’, ’63’, np.nan, ’94’, ’83’],

        ‘science’: [’88’, np.nan, ’66’, ’94’, np.nan],

        ‘english’: [’93’, ’74’, ’84’, ’92’, ’87’]}

    # Converting into dataframe

    df = pd.DataFrame(data_set)

    df2 = pd.DataFrame(data_set2)

    df3 = pd.DataFrame(data_set3)

    with pd.ExcelWriter(‘multiplesheet.xlsx’, engine=‘xlsxwriter’) as writer:

        df.to_excel(writer, sheet_name=‘Sheet’)

        df2.to_excel(writer, sheet_name=‘Sheet2’)

        df3.to_excel(writer, sheet_name=‘Sheet3’)

    print(‘Please check out the multiplesheet.xlsx file.’)

    Выход:

    Запись нескольких DataFrames на несколько листов

    Вы можете видеть, что есть три листа, и каждый лист имеет разные столбцы имени.

    Функция to_excel() принимает имя листа в качестве параметра, и здесь мы можем передать три разных имени листа, и этот DataFrame сохраняется на соответствующих листах.

    Заключение

    Как использовать метод Pandas.ExcelWriter в Python

    Если вы хотите экспортировать Pandas DataFrame в файлы Excel, вам нужен только класс ExcelWriter(). Класс ExcelWrite() предоставляет объект записи, а затем мы можем использовать функцию to_excel() для экспорта DataFrame в файл Excel.

    Last Updated on July 14, 2022 by

    This short article is part of the Transition from Excel to Python series. Last time we learned how to save one sheet into an Excel file. Now we’ll save multiple sheets to one (the same) Excel file using Python pandas.

    Just a reminder: df stands for dataframe, and pd is short for pandas.

    We’ll still use the df.to_excel() method, but we’ll need help from another class pd.ExcelWriter(). As its name suggests, this class writes to Excel files. If you read carefully the pd.to_excel() documentation, the ExcelWriter is actually the first argument.

    Mock-up dataframes

    Let’s create some mock-up dataframes, so we have something to work with. We create 2 dataframes, the first one is a 20 row by 10 columns random numbers; and the second dataframe is 10 rows by 1 column.

    import pandas as pd
    import numpy as np
    
    
    df_1 = pd.DataFrame(np.random.rand(20,10))
    df_2 = pd.DataFrame(np.random.rand(10,1))
    

    We’ll go through 2 methods of saving multi-sheets Excel files. The idea is pretty much the same between the 2 methods: we create an ExcelWriter, then pass it into the df.to_excel() for saving dataframe into an Excel file. The two methods are slightly different in syntax but they work the same way.

    Method 1

    This is the method demonstrated on the official pandas documentation.

    with pd.ExcelWriter('mult_sheets_1.xlsx') as writer1:
        df_1.to_excel(writer1, sheet_name = 'df_1', index = False)
        df_2.to_excel(writer1, sheet_name = 'df_2', index = False)
    

    Method 2

    This is my personal preferred method. Let me show you how it looks like then tell you why I prefer this over method 1.

    writer2 = pd.ExcelWriter('mult_sheets_2.xlsx')
    
    df_1.to_excel(writer2, sheet_name = 'df_1', index = False)
    df_2.to_excel(writer2, sheet_name = 'df_2', index = False)
    
    writer2.save()

    By now you must think that these two methods are the same! Well, yes and no.

    It’s true that both methods do exactly the same thing – saving the two dataframes into a single Excel file. however, the mechanism is quite different.

    The difference

    First of all, because of the with block in method 1, all your dataframes have to be in the same scope. This means if you have a dataframe outside of the current scope, you will have to bring it in first.

    Whereas for method 2, the dataframes can be in different scopes and it will still work. This is especially useful when your code is complicated.

    We will save data from Pandas DataFrame to Excel file.

    import pandas as pd 
    my_dict={
    	'NAME':['Ravi','Raju','Alex'],
    	'ID':[1,2,3],'MATH':[30,40,50],
    	'ENGLISH':[20,30,40]
    	}
    df = pd.DataFrame(data=my_dict)
    df.to_excel('D:my_file.xlsx')
    • to_excel() : DataFrame to excel file

    By default we will have index as left most column. We can remove index by using option index=False

    df.to_excel('D:my_file.xlsx',index=False)

    Storing Path

    We can keep in D drive ( root )

    df.to_excel('D:my_file.xlsx')

    Inside data directory

    df.to_excel('D:datamy_file.xlsx')

    Storing different worksheets

    Excel has worksheets to store data in different sheets.

    When we create a new excel file the data stored in Sheet1. We can create different Sheets and store data in different worksheets.

    By using sheet_name we can store in worksheet with specific name sheet_name=’my_Sheet_1′

    df.to_excel('D:my_file.xlsx',index=False,sheet_name='my_Sheet_1')

    This will store our data in my_sheet_1 worksheet of file name my_file.xlsx at root of D drive.

    multiple worksheets

    We will use one object of ExcelWriter to create multiple worksheets in a excel file.

    import pandas as pd 
    my_dict={
    	'NAME':['Ravi','Raju','Alex'],
    	'ID':[1,2,3],'MATH':[30,40,50],
    	'ENGLISH':[20,30,40]
    	}
    df = pd.DataFrame(data=my_dict)
    df2 = df.copy() # copy of df
    
    with pd.ExcelWriter('D:my_file.xlsx') as my_excel_obj: #Object created
        df.to_excel(my_excel_obj,sheet_name='my_Sheet_1')
        df2.to_excel(my_excel_obj,sheet_name='my_Sheet_2')

    Above code will create excel file with two worksheets. Here the new file name is my_file.xlsx with two worksheets.

    Appending worksheets

    We will add two more worksheets to the existing files by opening the file in append mode.

    Note that we are using the same my_file.xlsx file created in above code.

    We will be using mode=’a’ and engine=’openpyxl’ while creating the ExcelWriter object.

    import pandas as pd 
    my_dict={
    	'NAME':['Ravi','Raju','Alex'],
    	'ID':[1,2,3],'MATH':[30,40,50],
    	'ENGLISH':[20,30,40]
    	}
    df = pd.DataFrame(data=my_dict)
    df2 = df.copy() # copy of df
    
    with pd.ExcelWriter('D:my_file.xlsx',mode='a',engine='openpyxl') as my_excel_obj:
        df.to_excel(my_excel_obj,sheet_name='my_Sheet_3')
        df2.to_excel(my_excel_obj,sheet_name='my_Sheet_4')

    This code will add two more worksheets my_Sheet_3 and my_Sheet_4 to existing workbook my_file.xlsx .

    While executing the above code, you may get error saying Append mode is not supported with xlsxwriter. To solve this issue use engine=’openpyxl’ while creating the ExcelWriter object.

    Data from MySQL table

    We can read data from MySQL table and then store them in excel file.

    import mysql.connector
    import pandas as pd 
    my_connect = mysql.connector.connect(
          host="localhost",
          user="userid",
          passwd="password",
          database="my_tutorial"
        )
    ####### end of connection ####
    sql="SELECT * FROM student "
    df = pd.read_sql(sql,my_connect )
    
    df.to_excel('D:my_file.xlsx')

    In above code we have first connected to MySQL database and then collected the records of student table by using read_sql() to a DataFrame. Finally we saved them in an excel file using to_excel().

    Using SQLAlchemy MySQL connection

    Read more on MySQL with SQLAlchemy connection. Below code will create student.xlsx file in the same directory, you can add path ( as above ) if you want the file to be created at different location.

    import pandas as pd 
    from sqlalchemy import create_engine
    my_conn = create_engine("mysql+mysqldb://userid:pw@localhost/my_db")
    sql="SELECT * FROM student "
    df = pd.read_sql(sql,my_conn)
    df.to_excel('D:\my_data\student.xlsx') # Add the path

    We can collect data of different classes from our student table and store them in one excel file by keeping in different worksheets. So all students of class Four will be in one worksheet named as Four and similarly another worksheet for class Three students. You can extend this to other classes also.

    import pandas as pd 
    from sqlalchemy import create_engine
    my_conn = create_engine("mysql+mysqldb://userid:pw@localhost/my_db")
    sql="SELECT * FROM student WHERE class='Three'"
    sql2="SELECT * FROM student WHERE class='Four'"
    df=pd.read_sql(sql,my_conn)   # class Three students 
    df2=pd.read_sql(sql2,my_conn) # class Four students
    with pd.ExcelWriter('D:\my_data\student.xlsx',engine='openpyxl') as my_obj:
        df.to_excel(my_obj,sheet_name='Three')
        df2.to_excel(my_obj,sheet_name='Four')

    More about xlsxwriter

    Separator sep

    By default sep=»,» , This is one char length strig used to spearate data in a row.

    df.to_excel('D:my_file.xlsx',sep='#',index=False)

    na_rep Blank data

    How to handle if data is blank, we can use na_rep=’*’

    df.to_excel('D:my_file.xlsx',na_rep='*')

    Storing part of the data

    We can filter the DataFrame and then save the rows in xlsx file. For this we will use our test.csv file as we have more rows.

    Now let us store only two columns, class and name

    import pandas as pd 
    df=pd.read_csv('test.csv')
    df=df.loc[:,['class','name']]
    df = pd.DataFrame(data=df)
    df.to_excel('my_file.xlsx',index=False)

    We can use various other filters to manage the data and store in CSV file. You can rad more on filters sections.

    Data input and output from Pandas DataFrame

    Pandas
    read_csv
    to_csv
    read_excel
    to_string()

    To save Pandas DataFrames into multiple excel sheets, we can use the pd.ExcelWriter() method. Make sure you have the openpyxl package installed before using ExcelWriter().

    Steps

    • Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df1.
    • Print the input DataFrame, df1.
    • Create another DataFrame, df2, and print it.
    • Use ExcelWriter() method to write the given formatted cells into an Excel sheet.

    Example

    import pandas as pd
    
    df1 = pd.DataFrame(
       [[5, 2], [4, 1]],
       index=["One", "Two"],
       columns=["Rank", "Subjects"]
    )
    
    df2 = pd.DataFrame(
       [[15, 21], [41, 11]],
       index=["One", "Two"],
       columns=["Rank", "Subjects"]
    )
    
    print "DataFrame 1:n", df1
    print "DataFrame 2:n", df2
    
    with pd.ExcelWriter('output.xlsx') as writer:
    df1.to_excel(writer, sheet_name='Sheet_name_1')
    df2.to_excel(writer, sheet_name='Sheet_name_2')

    Output

    DataFrame 1:
    
         Rank  Subjects
    One    5     2
    Two    4     1
    
    DataFrame 2:
    
         Rank Subjects
    One   15    21
    Two   41    11

    It will also create an Excel file called «output.xlsx» in your project directory and save the DataFrame values in two different sheets.

    Use pandas to_excel() function to write a DataFrame to an excel sheet with extension .xlsx. By default it writes a single DataFrame to an excel file, you can also write multiple sheets by using an ExcelWriter object with a target file name, and sheet name to write to.

    Note that creating an ExcelWriter object with a file name that already exists will result in the contents of the existing file being erased.

    Related: pandas read Excel Sheet

    pandas to Excel key Points

    • By default, it uses xlsxwriter if it is installed otherwise it uses openpyxl
    • Supports saving multiple DataFrames to single sheet.
    • Save multiple sheets, append existing sheet or file.
    • Use ExcelWriter()

    Let’s create a pandas DataFrame from list and explore usingto_excel() function by using multiple parameters.

    
    import pandas as pd
    import numpy as np
    
    # Create multiple lists
    technologies =  ['Spark','Pandas','Java','Python', 'PHP']
    fee = [25000,20000,15000,15000,18000]
    duration = ['5o Days','35 Days',np.nan,'30 Days', '30 Days']
    discount = [2000,1000,800,500,800]
    columns=['Courses','Fee','Duration','Discount']
    
    # Create DataFrame from multiple lists
    df = pd.DataFrame(list(zip(technologies,fee,duration,discount)), columns=columns)
    print(df)
    
    # Outputs
    #  Courses    Fee Duration  Discount
    #0   Spark  25000  5o Days      2000
    #1  Pandas  20000  35 Days      1000
    #2    Java  15000      NaN       800
    #3  Python  15000  30 Days       500
    #4     PHP  18000  30 Days       800
    

    1. pandas DataFrame to Excel

    Use to_excel() function to write or export pandas DataFrame to excel sheet with extension xslx. Using this you can write excel files to the local file system, S3 e.t.c. Not specifying any parameter it default writes to a single sheet.

    to_excel() takes several optional params that can be used skip columns, skip rows, not to write index, set column names, formatting, and many more.

    
    # Write DataFrame to Excel file
    df.to_excel('Courses.xlsx')
    

    This creates an excel file with the contents as below. By default, It exports column names, indexes, and data to a sheet named 'Sheet1'.

    You can change the name of the sheet from Sheet1 to something that makes sense to your data by using sheet_name param. The below example exports it to the sheet named ‘Technologies‘.

    
    # Write DataFrame to Excel file with sheet name
    df.to_excel('Courses.xlsx', sheet_name='Technologies')
    

    2. Write to Multiple Sheets

    The ExcelWriter class allows you to write or export multiple pandas DataFrames to separate sheets. First, you need to create an object for ExcelWriter.

    The below example writes data from df object to a sheet named Technologies and df2 object to a sheet named Schedule.

    
    # Write to Multiple Sheets
    with pd.ExcelWriter('Courses.xlsx') as writer:
        df.to_excel(writer, sheet_name='Technologies')
        df2.to_excel(writer, sheet_name='Schedule')
    

    3. Append to Existing Excel File

    ExcelWriter can be used to append DataFrame to an excel file. Use mode param with value 'a' to append. The code below opens an existing file and adds data from the DataFrame to the specified sheet.

    
    # Append DataFrame to existing excel file
    with pd.ExcelWriter('Courses.xlsx',mode='a') as writer:  
        df.to_excel(writer, sheet_name='Technologies')
    

    4. Save Selected Columns

    use param columns to save selected columns from DataFrame to excel file. The below example only saves columns Fee, Duration to excel file.

    
    # Save Selected Columns to Excel File
    df.to_excel('Courses.xlsx', columns = ['Fee','Duration'])
    

    Use header param with a list of values if you wanted to write with different column names.

    5. Skip Index

    To skip Index from writing use index=False param. By default, it is set to True meaning write numerical Index to excel sheet.

    
    # Skip Index
    df.to_excel('Courses.xlsx', index = False)
    

    Conclusion

    In this article, you have learned how to write pandas DataFrame to excel file by using to_excel(). Also explored how to write to specific sheets, multiple sheets, and append to existing excel file.

    Related Articles

    • pandas ExcelWriter Usage with Examples
    • pandas write CSV file
    • pandas read Excel
    • Pandas ExcelWriter Explained with Examples
    • Pandas Read Multiple CSV Files into DataFrame
    • How to Read Excel Multiple Sheets in Pandas
    • Pretty Print Pandas DataFrame or Series?
    • Pandas Handle Missing Data in Dataframe
    • How to read CSV without headers in pandas

    References

    • https://stackoverflow.com/questions/38074678/append-existing-excel-sheet-with-new-dataframe-using-python-pandas/38075046#38075046

    Like this post? Please share to your friends:
  • To end a friendship word
  • To overcome obstacles word
  • To eat one s word to save your breath
  • To or from csv excel php
  • To eat like an animal word