Adventures in Machine Learning

Exporting Data from Python to MS Excel with Pandas: A Complete Guide

Exporting Data from Python to MS Excel Using Pandas

Pandas is a popular library in the Python programming language used for data manipulation and data analysis. With Pandas, it is easy to create dataframes and export them to a variety of formats, including MS Excel.

In this article, we will discuss the steps involved in exporting data from Python to MS Excel using Pandas.

Importing the Pandas Library

Before we start, we need to make sure that we have the Pandas library installed. If it is not already installed, we can install it using pip, the package installer for Python.

To install the Pandas library, open a terminal and type in the following command:

$ pip install pandas

Once we have the library installed, we can import it into our Python script using the following command:

import pandas as pd

Creating Sample Data to Export

To export data to MS Excel, we first need to have some data to export. Let’s create some sample data that we can use to demonstrate the process.

For this example, let’s say we have data on internet service providers, their data speeds, monthly prices, and data limits. We can create a Pandas dataframe to store this data using the following code:

data = {‘Internet Service Providers’: [‘AT&T’, ‘Comcast’, ‘Verizon’, ‘Spectrum’],

‘Data Speeds’: [’50 Mbps’, ‘100 Mbps’, ‘150 Mbps’, ‘200 Mbps’],

‘Monthly Prices’: [‘$50’, ‘$70’, ‘$90’, ‘$110’],

‘Data Limits’: [‘1 TB’, ‘1.2 TB’, ‘1.5 TB’, ‘2 TB’]}

df = pd.DataFrame(data)

Adding the Data to a Dataframe

Now that we have our data, we can add it to a Pandas dataframe using the following code:

df = pd.DataFrame(data)

This creates a new dataframe named ‘df’ that contains our data.

Setting the Indexes Manually

By default, Pandas indexes dataframes using integers starting from zero. However, in some cases, we might want to set our own indexes.

For instance, if we have data for different months, we might want to set the index to the name of the month. To set the index to ‘May 2022’, we can use the following code:

df.index = [‘May 2022’]*len(df)

This sets the index of our dataframe to ‘May 2022’ for each row.

Exporting Pandas Dataframe to Excel

To export our Pandas dataframe to MS Excel, we can use the ‘to_excel()’ command. We will need to specify the file address where we want to save the Excel file, the sheet name, the starting row, and the starting column.

Here’s an example:

df.to_excel(‘data.xlsx’, sheet_name=’Internet Providers’, startrow=0, startcol=0)

This exports the contents of our dataframe to an Excel file named ‘data.xlsx’. The data is saved to a sheet named ‘Internet Providers’, starting from row 0, column 0.

Bonus Steps Customizing the Sheet and the Starting Row and Column to Export

With a few extra steps, we can customize the exported sheet by adding column headers, formatting the cells, and starting the data from a specific row and column. Here’s an example:

from openpyxl import Workbook

# Create a new workbook

workbook = Workbook()

# Select the active worksheet

worksheet =

# Add column headers

worksheet[‘A1’] = ‘Internet Service Providers’

worksheet[‘B1’] = ‘Data Speeds’

worksheet[‘C1’] = ‘Monthly Prices’

worksheet[‘D1’] = ‘Data Limits’

# Apply formatting to the cells

for row in worksheet.iter_rows(min_row=1, max_row=1):

for cell in row:

cell.font = cell.font.copy(bold=True)

# Copy the dataframe to the worksheet

for r in dataframe_to_rows(df, index=False, header=False):


# Save the workbook‘Internet Providers.xlsx’)

This code creates a new workbook and selects the active worksheet. It then adds column headers to the first row of the worksheet and applies formatting to make the headers bold.

Finally, it copies the contents of the dataframe to the worksheet, starting from the second row.

Exploring Other Dedicated Tools for Presenting Data

While Pandas is a powerful tool for data manipulation and analysis, it has some limitations when it comes to presenting data. For instance, it can be challenging to create complex charts and graphs.

In such cases, it might be helpful to use other dedicated tools for presenting data. MS Excel is a popular tool for presenting data.

It has a wide range of features for creating charts, graphs, and tables, and it is widely used in many industries. It also allows for more user-friendly data visualization and can present data in a way that non-technical people can understand.

There are other dedicated tools available for presenting data, such as Tableau, QlikView, and Power BI. These tools provide more advanced visualization features than Excel and can handle larger datasets.


Exporting data from Python to MS Excel using Pandas is a straightforward and powerful process. With just a few lines of code, we can export data to an Excel file that can be easily shared and analyzed by others.

However, if we need more advanced visualization, we might want to explore other dedicated tools for presenting data. In this article, we discussed the process of exporting data from Python to MS Excel using the Pandas library.

We covered important steps such as importing the Pandas library, creating sample data, adding data to a dataframe, setting indexes manually, and exporting to Excel. Additionally, we explored other dedicated tools such as MS Excel, Tableau, QlikView, and Power BI to present data.

By using these tools, we can create more user-friendly data visualization and can present data in a way that non-technical people can understand. Overall, this article demonstrated the importance of exporting data from Python to MS Excel and exploring other dedicated tools for presenting data to help us better communicate our insights.

Popular Posts