Pandas dataframe save to excel

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

Improve Article

Save Article

Like Article

  • Read
  • Discuss
  • Improve Article

    Save Article

    Like Article

    Let us see how to export a Pandas DataFrame to an Excel file. 

    Algorithm:

    1. Create the DataFrame.
    2. Determine the name of the Excel file.
    3. Call to_excel() function with the file name to export the DataFrame.

    Example 1:

    Python3

    import pandas as pd

    marks_data = pd.DataFrame({'ID': {0: 23, 1: 43, 2: 12,

                                     3: 13, 4: 67, 5: 89,

                                     6: 90, 7: 56, 8: 34},

                              'Name': {0: 'Ram', 1: 'Deep',

                                       2: 'Yash', 3: 'Aman',

                                       4: 'Arjun', 5: 'Aditya',

                                       6: 'Divya', 7: 'Chalsea',

                                       8: 'Akash' },

                              'Marks': {0: 89, 1: 97, 2: 45, 3: 78,

                                        4: 56, 5: 76, 6: 100, 7: 87,

                                        8: 81},

                              'Grade': {0: 'B', 1: 'A', 2: 'F', 3: 'C',

                                        4: 'E', 5: 'C', 6: 'A', 7: 'B',

                                        8: 'B'}})

    file_name = 'MarksData.xlsx'

    marks_data.to_excel(file_name)

    print('DataFrame is written to Excel File successfully.')

    Output:

    DataFrame is written to Excel File successfully.

    The Excel file is:

    Example 2: We can also first use the ExcelWriter() method to save it.

    Python3

    import pandas as pd

    cars_data = pd.DataFrame({'Cars': ['BMW', 'Audi', 'Bugatti'

                                       'Porsche', 'Volkswagen'],

                              'MaxSpeed': [220, 230, 240, 210, 190],

                              'Color': ['Black', 'Red', 'Blue'

                                        'Violet', 'White']})

    datatoexcel = pd.ExcelWriter('CarsData1.xlsx')

    cars_data.to_excel(datatoexcel)

    datatoexcel.save()

    print('DataFrame is written to Excel File successfully.')

    Output:

    DataFrame is written to Excel File successfully.

    Like Article

    Save Article

    • Редакция Кодкампа

    17 авг. 2022 г.
    читать 2 мин


    Часто вас может заинтересовать экспорт фрейма данных pandas в Excel. К счастью, это легко сделать с помощью функции pandas to_excel() .

    Чтобы использовать эту функцию, вам нужно сначала установить openpyxl , чтобы вы могли записывать файлы в Excel:

    pip install openpyxl
    

    В этом руководстве будет объяснено несколько примеров использования этой функции со следующим фреймом данных:

    import pandas as pd
    
    #create DataFrame
    df = pd.DataFrame({'points': [25, 12, 15, 14, 19],
     'assists': [5, 7, 7, 9, 12],
     'rebounds': [11, 8, 10, 6, 6]}) 
    
    #view DataFrame
    df
    
     points assists rebounds
    0 25 5 11
    1 12 7 8
    2 15 7 10
    3 14 9 6
    4 19 12 6
    

    Пример 1: базовый экспорт

    В следующем коде показано, как экспортировать DataFrame по определенному пути к файлу и сохранить его как mydata.xlsx :

    df.to_excel (r'C:UsersZachDesktopmydata.xlsx')
    

    Вот как выглядит фактический файл Excel:

    Пример 2: Экспорт без индекса

    В следующем коде показано, как экспортировать DataFrame в определенный путь к файлу и удалить столбец индекса:

    df.to_excel (r'C:UsersZachDesktopmydata.xlsx', index= False )
    

    Вот как выглядит фактический файл Excel:

    Пример 3: Экспорт без индекса и заголовка

    В следующем коде показано, как экспортировать DataFrame в определенный путь к файлу и удалить столбец индекса и строку заголовка:

    df.to_excel (r'C:UsersZachDesktopmydata.xlsx', index= False, header= False )
    

    Вот как выглядит фактический файл Excel:

    Пример 4: Экспорт и имя листа

    В следующем коде показано, как экспортировать DataFrame в определенный путь к файлу и назвать рабочий лист Excel:

    df.to_excel (r'C:UsersZachDesktopmydata.xlsx', sheet_name='this_data')
    

    Вот как выглядит фактический файл Excel:

    Полную документацию по функции to_excel() можно найти здесь .

    I am trying to load data from the web source and save it as a Excel file but not sure how to do it. What should I do?

    import requests
    import pandas as pd
    import xmltodict
    
    
    url = "https://www.kstan.ua/sitemap.xml"
    res = requests.get(url)
    raw = xmltodict.parse(res.text)
    
    data = [[r["loc"], r["lastmod"]] for r in raw["urlset"]["url"]]
    print("Number of sitemaps:", len(data))
    df = pd.DataFrame(data, columns=["links", "lastmod"])
    

    lv10's user avatar

    lv10

    1,4397 gold badges25 silver badges46 bronze badges

    asked Mar 14, 2019 at 19:08

    Игорь's user avatar

    1

    df.to_csv("output.csv", index=False)
    

    OR

    df.to_excel("output.xlsx")
    

    answered Mar 14, 2019 at 19:11

    mujjiga's user avatar

    mujjigamujjiga

    15.9k2 gold badges30 silver badges47 bronze badges

    0

    You can write the dataframe to excel using the pandas ExcelWriter, such as this:

    import pandas as pd
    with pd.ExcelWriter('path_to_file.xlsx') as writer:
        dataframe.to_excel(writer)
    

    CodyF's user avatar

    CodyF

    4,8773 gold badges25 silver badges38 bronze badges

    answered Mar 14, 2019 at 19:20

    razimbres's user avatar

    razimbresrazimbres

    4,5185 gold badges23 silver badges49 bronze badges

    If you want to create multiple sheets in the same file

    with pd.ExcelWriter('csv_s/results.xlsx') as writer:
       same_res.to_excel(writer, sheet_name='same')
       diff_res.to_excel(writer, sheet_name='sheet2')
    

    answered Nov 29, 2022 at 17:01

    Mohit Gaikwad's user avatar

    Вы можете сохранить или записать DataFrame в файл Excel или конкретный лист в файле Excel, используя метод pandas.DataFrame.to_excel() класса DataFrame.

    В этом руководстве мы узнаем, как записать DataFrame Pandas в файл Excel с помощью примеров программ Python.

    Необходимым условием для работы с функциями файлов Excel в pandas является установка модуля openpyxl. Чтобы установить openpyxl с помощью pip, выполните следующую команду.

    pip install openpyxl

    Пример 1

    Вы можете записать DataFrame в файл Excel без упоминания имени листа. Пошаговый процесс представлен ниже:

    1. Подготовьте свой DataFrame. В этом примере мы инициализируем DataFrame с несколькими строками и столбцами.
    2. Создайте модуль записи Excel с именем выходного файла Excel, в который вы хотите записать наш DataFrame.
    3. Вызов функции to_excel() в DataFrame с помощью модуля записи Excel, переданного в качестве аргумента.
    4. Сохраните файл Excel, используя метод save() Excel Writer.
    import pandas as pd
    
    # create dataframe
    df_marks = pd.DataFrame({'name': ['Somu', 'Kiku', 'Amol', 'Lini'],
         'physics': [68, 74, 77, 78],
         'chemistry': [84, 56, 73, 69],
         'algebra': [78, 88, 82, 87]})
    
    # create excel writer object
    writer = pd.ExcelWriter('output.xlsx')
    # write dataframe to excel
    df_marks.to_excel(writer)
    # save the excel
    writer.save()
    print('DataFrame is written successfully to Excel File.')

    Запустите указанную выше программу, и файл Excel будет создан с именем, указанным при создании модуля записи Excel.

    Выходной файл Excel

    Откройте файл Excel, и вы увидите индекс, метки столбцов и данные строк, записанные в файл.

    Запись DataFrame в лист Excel

    Вы можете записать DataFrame на конкретный лист Excel. Пошаговый процесс:

    1. Подготовьте свой DataFrame.
    2. Создайте модуль записи Excel с именем желаемого выходного файла Excel.
    3. Вызовите функцию to_excel() в DataFrame с записывающим устройством и именем листа Excel, переданными в качестве аргументов.
    4. Сохраните файл Excel, используя метод save() Excel Writer.
    import pandas as pd
    
    # create dataframe
    df_marks = pd.DataFrame({'name': ['Somu', 'Kiku', 'Amol', 'Lini'],
         'physics': [68, 74, 77, 78],
         'chemistry': [84, 56, 73, 69],
         'algebra': [78, 88, 82, 87]})
    
    # create excel writer
    writer = pd.ExcelWriter('output.xlsx')
    # write dataframe to excel sheet named 'marks'
    df_marks.to_excel(writer, 'marks')
    # save the excel file
    writer.save()
    print('DataFrame is written successfully to Excel Sheet.')

    Запустите программу:

    Выходной файл Excel

    Откройте файл Excel. Обратите внимание на название листа Excel. Он назван в честь строки, которую мы указали в качестве второго аргумента функции to_excel().

    Работа в листе Excel

    В этом руководстве по Pandas мы узнали, как написать Pandas DataFrame в лист Excel с помощью примеров программ Python.

    This div height required for enabling the sticky sidebar

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

    Write Excel with Python Pandas. You can write any data (lists, strings, numbers etc) to Excel, by first converting it into a Pandas DataFrame and then writing the DataFrame to Excel.

    To export a Pandas DataFrame as an Excel file (extension: .xlsx, .xls), use the to_excel() method.

    Related course: Data Analysis with Python Pandas

    installxlwt, openpyxl

    to_excel() uses a library called xlwt and openpyxl internally.

    • xlwt is used to write .xls files (formats up to Excel2003)
    • openpyxl is used to write .xlsx (Excel2007 or later formats).

    Both can be installed with pip. (pip3 depending on the environment)

    1
    2
    $ pip install xlwt
    $ pip install openpyxl

    Write Excel

    Write DataFrame to Excel file

    Importing openpyxl is required if you want to append it to an existing Excel file described at the end.
    A dataframe is defined below:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    import pandas as pd
    import openpyxl

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

    print(df)




    You can specify a path as the first argument of the to_excel() method.

    Note: that the data in the original file is deleted when overwriting.

    The argument new_sheet_name is the name of the sheet. If omitted, it will be named Sheet1.

    1
    df.to_excel('pandas_to_excel.xlsx', sheet_name='new_sheet_name')

    Python Write Excel

    Related course: Data Analysis with Python Pandas

    If you do not need to write index (row name), columns (column name), the argument index, columns is False.

    1
    df.to_excel('pandas_to_excel_no_index_header.xlsx', index=False, header=False)

    Write multiple DataFrames to Excel files

    The ExcelWriter object allows you to use multiple pandas. DataFrame objects can be exported to separate sheets.

    As an example, pandas. Prepare another DataFrame object.

    1
    2
    3
    4
    5
    6
    df2 = df[['a', 'c']]
    print(df2)




    Then use the ExcelWriter() function like this:

    1
    2
    3
    with pd.ExcelWriter('pandas_to_excel.xlsx') as writer:
    df.to_excel(writer, sheet_name='sheet1')
    df2.to_excel(writer, sheet_name='sheet2')

    You don’t need to call writer.save(), writer.close() within the blocks.

    Append to an existing Excel file

    You can append a DataFrame to an existing Excel file. The code below opens an existing file, then adds two sheets with the data of the dataframes.

    Note: Because it is processed using openpyxl, only .xlsx files are included.

    1
    2
    3
    4
    5
    6
    path = 'pandas_to_excel.xlsx'

    with pd.ExcelWriter(path) as writer:
    writer.book = openpyxl.load_workbook(path)
    df.to_excel(writer, sheet_name='new_sheet1')
    df2.to_excel(writer, sheet_name='new_sheet2')

    Related course: Data Analysis with Python Pandas

    Время на прочтение
    7 мин

    Количество просмотров 172K

    Если Вы только начинаете свой путь знакомства с возможностями Python, ваши познания еще имеют начальный уровень — этот материал для Вас. В статье мы опишем, как можно извлекать информацию из данных, представленных в Excel файлах, работать с ними используя базовый функционал библиотек. В первой части статьи мы расскажем про установку необходимых библиотек и настройку среды. Во второй части — предоставим обзор библиотек, которые могут быть использованы для загрузки и записи таблиц в файлы с помощью Python и расскажем как работать с такими библиотеками как pandas, openpyxl, xlrd, xlutils, pyexcel.

    В какой-то момент вы неизбежно столкнетесь с необходимостью работы с данными Excel, и нет гарантии, что работа с таким форматами хранения данных доставит вам удовольствие. Поэтому разработчики Python реализовали удобный способ читать, редактировать и производить иные манипуляции не только с файлами Excel, но и с файлами других типов.

    Отправная точка — наличие данных

    ПЕРЕВОД
    Оригинал статьи — www.datacamp.com/community/tutorials/python-excel-tutorial
    Автор — Karlijn Willems

    Когда вы начинаете проект по анализу данных, вы часто сталкиваетесь со статистикой собранной, возможно, при помощи счетчиков, возможно, при помощи выгрузок данных из систем типа Kaggle, Quandl и т. д. Но большая часть данных все-таки находится в Google или репозиториях, которыми поделились другие пользователи. Эти данные могут быть в формате Excel или в файле с .csv расширением.

    Данные есть, данных много. Анализируй — не хочу. С чего начать? Первый шаг в анализе данных — их верификация. Иными словами — необходимо убедиться в качестве входящих данных.
    В случае, если данные хранятся в таблице, необходимо не только подтвердить качество данных (нужно быть уверенным, что данные таблицы ответят на поставленный для исследования вопрос), но и оценить, можно ли доверять этим данным.

    Проверка качества таблицы

    Чтобы проверить качество таблицы, обычно используют простой чек-лист. Отвечают ли данные в таблице следующим условиям:

    • данные являются статистикой;
    • различные типы данных: время, вычисления, результат;
    • данные полные и консистентные: структура данных в таблице — систематическая, а присутствующие формулы — работающие.

    Ответы на эти простые вопросы позволят понять, не противоречит ли ваша таблица стандарту. Конечно, приведенный чек-лист не является исчерпывающим: существует много правил, на соответствие которым вы можете проверять данные в таблице, чтобы убедиться, что таблица не является “гадким утенком”. Однако, приведенный выше чек-лист наиболее актуален, если вы хотите убедиться, что таблица содержит качественные данные.

    Бест-практикс табличных данных

    Читать данные таблицы при помощи Python — это хорошо. Но данные хочется еще и редактировать. Причем редактирование данных в таблице, должно соответствовать следующим условиям:

    • первая строка таблицы зарезервирована для заголовка, а первый столбец используется для идентификации единицы выборки;
    • избегайте имен, значений или полей с пробелами. В противном случае, каждое слово будет интерпретироваться как отдельная переменная, что приведет к ошибкам, связанным с количеством элементов в строке в наборе данных. Лучше использовать подчеркивания, регистр (первая буква каждого раздела текста — заглавная) или соединительные слова;
    • отдавайте предпочтение коротким названиям;
    • старайтесь избегать использования названий, которые содержат символы ?, $,%, ^, &, *, (,),-,#, ?,,,<,>, /, |, , [ ,] ,{, и };
    • удаляйте любые комментарии, которые вы сделали в файле, чтобы избежать дополнительных столбцов или полей со значением NA;
    • убедитесь, что любые недостающие значения в наборе данных отображаются как NA.

    После внесения необходимых изменений (или когда вы внимательно просмотрите свои данные), убедитесь, что внесенные изменения сохранены. Это важно, потому что позволит еще раз взглянуть на данные, при необходимости отредактировать, дополнить или внести изменения, сохраняя формулы, которые, возможно, использовались для расчета.

    Если вы работаете с Microsoft Excel, вы наверняка знаете, что есть большое количество вариантов сохранения файла помимо используемых по умолчанию расширения: .xls или .xlsx (переходим на вкладку “файл”, “сохранить как” и выбираем другое расширение (наиболее часто используемые расширения для сохранения данных с целью анализа — .CSV и.ТХТ)). В зависимости от варианта сохранения поля данных будут разделены знаками табуляции или запятыми, которые составляют поле “разделитель”. Итак, данные проверены и сохранены. Начинаем готовить рабочее пространство.

    Подготовка рабочего пространства

    Подготовка рабочего пространства — одна из первых вещей, которую надо сделать, чтобы быть уверенным в качественном результате анализа.

    Первый шаг — проверка рабочей директории.

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

    Для проверки дайте следующие команды:

    # Import `os` 
    import os
    
    # Retrieve current working directory (`cwd`)
    cwd = os.getcwd()
    cwd
    
    # Change directory 
    os.chdir("/path/to/your/folder")
    
    # List all files and directories in current directory
    os.listdir('.')

    Эти команды важны не только для загрузки данных, но и для дальнейшего анализа. Итак, вы прошли все проверки, вы сохранили данные и подготовили рабочее пространство. Уже можно начать чтение данных в Python? :) К сожалению пока нет. Нужно сделать еще одну последнюю вещь.

    Установка пакетов для чтения и записи Excel файлов

    Несмотря на то, что вы еще не знаете, какие библиотеки будут нужны для импорта данных, нужно убедиться, что у все готово для установки этих библиотек. Если у вас установлен Python 2> = 2.7.9 или Python 3> = 3.4, нет повода для беспокойства — обычно, в этих версиях уже все подготовлено. Поэтому просто убедитесь, что вы обновились до последней версии :)

    Для этого запустите в своем компьютере следующую команду:

    # For Linux/OS X
    pip install -U pip setuptools
    
    # For Windows
    python -m pip install -U pip setuptools

    В случае, если вы еще не установили pip, запустите скрипт python get-pip.py, который вы можете найти здесь (там же есть инструкции по установке и help).

    Установка Anaconda

    Установка дистрибутива Anaconda Python — альтернативный вариант, если вы используете Python для анализа данных. Это простой и быстрый способ начать работу с анализом данных — ведь отдельно устанавливать пакеты, необходимые для data science не придется.

    Это особенно удобно для новичков, однако даже опытные разработчики часто идут этим путем, ведь Anakonda — удобный способ быстро протестировать некоторые вещи без необходимости устанавливать каждый пакет отдельно.

    Anaconda включает в себя 100 наиболее популярных библиотек Python, R и Scala для анализа данных в нескольких средах разработки с открытым исходным кодом, таких как Jupyter и Spyder. Если вы хотите начать работу с Jupyter Notebook, то вам сюда.

    Чтобы установить Anaconda — вам сюда.

    Загрузка файлов Excel как Pandas DataFrame

    Ну что ж, мы сделали все, чтобы настроить среду! Теперь самое время начать импорт файлов.

    Один из способов, которым вы будете часто пользоваться для импорта файлов с целью анализа данных — импорт с помощью библиотеки Pandas (Pandas — программная библиотека на языке Python для обработки и анализа данных). Работа Pandas с данными происходит поверх библиотеки NumPy, являющейся инструментом более низкого уровня. Pandas — мощная и гибкая библиотека и она очень часто используется для структуризации данных в целях облегчения анализа.

    Если у вас уже есть Pandas в Anaconda, вы можете просто загрузить файлы в Pandas DataFrames с помощью pd.Excelfile ():

    # Import pandas
    import pandas as pd
    
    # Assign spreadsheet filename to `file`
    file = 'example.xlsx'
    
    # Load spreadsheet
    xl = pd.ExcelFile(file)
    
    # Print the sheet names
    print(xl.sheet_names)
    
    # Load a sheet into a DataFrame by name: df1
    df1 = xl.parse('Sheet1')

    Если вы не установили Anaconda, просто запустите pip install pandas, чтобы установить пакет Pandas в вашей среде, а затем выполните команды, приведенные выше.

    Для чтения .csv-файлов есть аналогичная функция загрузки данных в DataFrame: read_csv (). Вот пример того, как вы можете использовать эту функцию:

    # Import pandas
    import pandas as pd
    
    # Load csv
    df = pd.read_csv("example.csv") 

    Разделителем, который эта функция будет учитывать, является по умолчанию запятая, но вы можете, если хотите, указать альтернативный разделитель. Перейдите к документации, если хотите узнать, какие другие аргументы можно указать, чтобы произвести импорт.

    Как записывать Pandas DataFrame в Excel файл

    Предположим, после анализа данных вы хотите записать данные в новый файл. Существует способ записать данные Pandas DataFrames (с помощью функции to_excel ). Но, прежде чем использовать эту функцию, убедитесь, что у вас установлен XlsxWriter, если вы хотите записать свои данные на несколько листов в файле .xlsx:

    # Install `XlsxWriter` 
    pip install XlsxWriter
    
    # Specify a writer
    writer = pd.ExcelWriter('example.xlsx', engine='xlsxwriter')
    
    # Write your DataFrame to a file     
    yourData.to_excel(writer, 'Sheet1')
    
    # Save the result 
    writer.save()
    

    Обратите внимание, что в фрагменте кода используется объект ExcelWriter для вывода DataFrame. Иными словами, вы передаете переменную writer в функцию to_excel (), и указываете имя листа. Таким образом, вы добавляете лист с данными в существующую книгу. Также можно использовать ExcelWriter для сохранения нескольких разных DataFrames в одной книге.

    То есть если вы просто хотите сохранить один файл DataFrame в файл, вы можете обойтись без установки библиотеки XlsxWriter. Просто не указываете аргумент, который передается функции pd.ExcelWriter (), остальные шаги остаются неизменными.

    Подобно функциям, которые используются для чтения в .csv-файлах, есть также функция to_csv () для записи результатов обратно в файл с разделителями-запятыми. Он работает так же, как когда мы использовали ее для чтения в файле:

    # Write the DataFrame to csv
    df.to_csv("example.csv")

    Если вы хотите иметь отдельный файл с вкладкой, вы можете передать a t аргументу sep. Обратите внимание, что существуют различные другие функции, которые можно использовать для вывода файлов. Их можно найти здесь.

    Использование виртуальной среды

    Общий совет по установке библиотек — делать установку в виртуальной среде Python без системных библиотек. Вы можете использовать virtualenv для создания изолированных сред Python: он создает папку, содержащую все необходимое для использования библиотек, которые потребуются для Python.

    Чтобы начать работу с virtualenv, сначала нужно его установить. Потом перейти в директорию, где будет находится проект. Создать virtualenv в этой папке и загрузить, если нужно, в определенную версию Python. После этого активируете виртуальную среду. Теперь можно начинать загрузку других библиотек и начинать работать с ними.

    Не забудьте отключить среду, когда вы закончите!

    # Install virtualenv
    $ pip install virtualenv
    
    # Go to the folder of your project
    $ cd my_folder
    
    # Create a virtual environment `venv`
    $ virtualenv venv
    
    # Indicate the Python interpreter to use for `venv`
    $ virtualenv -p /usr/bin/python2.7 venv
    
    # Activate `venv`
    $ source venv/bin/activate
    
    # Deactivate `venv`
    $ deactivate
    

    Обратите внимание, что виртуальная среда может показаться сначала проблематичной, если вы делаете первые шаги в области анализа данных с помощью Python. И особенно, если у вас только один проект, вы можете не понимать, зачем вообще нужна виртуальная среда.

    Но что делать, если у вас несколько проектов, работающих одновременно, и вы не хотите, чтобы они использовали одну и ту же установку Python? Или если у ваших проектов есть противоречивые требования. В таких случаях виртуальная среда — идеальное решение.

    Во второй части статьи мы расскажем об основных библиотеках для анализа данных.
    Продолжение следует…

    Понравилась статья? Поделить с друзьями:
  • Pandas dataframe export excel
  • Pandas create excel file
  • Pandas add sheet to excel
  • Panda функция read excel
  • Palo excel что это такое