Import data from Python to Excel

It is not always possible to get the dataset in CSV format. So, Pandas provides us the functions to convert datasets in other formats to the Data frame. An excel file has a ‘.xlsx’ format. 

Before we get started,  we need to install a few libraries. 

pip install pandas
pip install xlrd

 For importing an Excel file into Python using Pandas we have to use pandas.read_excel() function.

Syntax: pandas.read_excel(io, sheet_name=0, header=0, names=None,….)

Return: DataFrame or dict of DataFrames.

Let’s suppose the Excel file looks like this:

Now, we can dive into the code. 

Example 1: Read an Excel file.

Python3




import pandas as pd

  

df= pd.read_excel("sample.xlsx")

printimport0

Output:

Example 2: To select a particular column, we can pass a parameter “index_col“. 

Python3




import pandas as pd

  

import4

df= pd.read_excel("sample.xlsx"import9

pandas as pd0pandas as pd1= pandas as pd3pandas as pd4

  

printimport0

Output:

Import data from Python to Excel

Example 3: In case you don’t prefer the initial heading of the columns, you can change it to indexes using the parameter “header”.

Python3




import pandas as pd

  

df= pd.read_excel( 4import9

pandas as pd0 7=  9)

printimport0

Output:

Example 4: If you want to change the data type of a particular column you can do it using the parameter “dtype“.

Python3




import pandas as pd

  

df= pd.read_excel( 4=0

pandas as pd0=2= =4=5=6=7import9

=9pd.read_excel(0=6pd.read_excel(2pd.read_excel(3

printimport0

Output:

Example 5: In case you have unknown values, then you can handle it using the parameter “na_values“. It will convert the mentioned unknown values into “NaN” 

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he has worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and Human Resource.

Learn to import data into Python from various sources, such as Excel, SQL, SAS and right from the web.

See DetailsRight Arrow

Start course

Python for Spreadsheet Users

Beginner

4 hr

21.7K

Use your knowledge of common spreadsheet functions and techniques to explore Python!

See DetailsRight Arrow

Start course

Pandas Joins for Spreadsheet Users

Beginner

4 hr

2.9K

Learn how to effectively and efficiently join datasets in tabular format using the Python Pandas library.

You can easily import an Excel file into Python using Pandas. In order to accomplish this goal, you’ll need to use read_excel:

import pandas as pd

df = pd.read_excel(r'Path where the Excel file is stored\File name.xlsx')
print(df)

Note that for an earlier version of Excel, you may need to use the file extension of ‘xls’

And if you have a specific Excel sheet that you’d like to import, you may then apply:

import pandas as pd

df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
print(df)

Let’s now review an example that includes the data to be imported into Python.

The Data to be Imported into Python

Suppose that you have the following table stored in Excel (where the Excel file name is ‘products‘):

product_namepricecomputer700tablet250printer120laptop1200keyboard100

You may then follow the steps below to import the Excel file into Python.

Steps to Import an Excel File into Python using Pandas

Step 1: Capture the file path

First, capture the full path where the Excel file is stored on your computer.

For example, let’s suppose that an Excel file is stored under the following path:

C:\Users\Ron\Desktop\products.xlsx

In the Python code below, you’ll need to modify the path name to reflect the location where the Excel file is stored on your computer.

Don’t forget to include the file name (in our example, it’s ‘products‘ as highlighted in blue). You’ll also need to include the Excel file extension (in our case, it’s ‘.xlsx‘ as highlighted in green).

Step 2: Apply the Python code

Here is the Python code for our example:

import pandas as pd

df = pd.read_excel(r'C:\Users\Ron\Desktop\products.xlsx')
print(df)

Note that you should place “r” before the path string to address special characters, such as ‘\’. In addition, don’t forget to put the file name at the end of the path + ‘.xlsx’

Step 3: Run the Python code to import the Excel file

Run the Python code (adjusted to your path), and you’ll get the following dataset:

  product_name  price
0     computer    700
1       tablet    250
2      printer    120
3       laptop   1200
4     keyboard    100

Notice that you got the same results as those that were stored in the Excel file.

Note: you’ll have to install an additional package if you get the following error when running the code:

ImportError: Missing optional dependency ‘xlrd’

You may then use the PIP install approach to install openpyxl for .xlsx files:

pip install openpyxl

Optional Step: Selecting subset of columns

Now what if you want to select a specific column or columns from the Excel file?

For example, what if you want to select only the product_name column? If that’s the case, you can specify this column name as captured below:

import pandas as pd

data = pd.read_excel(r'C:\Users\Ron\Desktop\products.xlsx') 
df = pd.DataFrame(data, columns=['product_name'])
print(df)

Run the code (after adjusting the file path), and you’ll get only the product_name column:

  product_name
0     computer
1       tablet
2      printer
3       laptop
4     keyboard

You can specify additional columns by separating their names using a comma, so if you want to include both the product_name and price columns, you can use this syntax:

import pandas as pd

data = pd.read_excel(r'C:\Users\Ron\Desktop\products.xlsx') 
df = pd.DataFrame(data, columns=['product_name', 'price'])
print(df)

You’ll need to make sure that the column names specified in the code exactly match with the column names within the Excel file. Otherwise, you’ll get NaN values.

Conclusion

You just saw how to import an Excel file into Python using Pandas.

At times, you may need to import a CSV file into Python. If that’s the case, you may want to check the following tutorial that explains how to import a CSV file into Python using Pandas.

You may also check the Pandas Documentation to find out more about the different options that you may apply in regards to read_excel.

How do I import data from Python to excel?

Steps to Import an Excel File into Python using Pandas.
Step 1: Capture the file path. First, capture the full path where the Excel file is stored on your computer. ... .
Step 2: Apply the Python code. ... .
Step 3: Run the Python code to import the Excel file..

How do I import a CSV file into Excel using Python?

Algorithm (Steps) load the CSV as a pandas data frame. Convert the CSV file to an excel file by passing the index as True as an argument to the excel() function and displaying index values. index=True means that index values are shown here. Use the save() function (saves the file) to save the result/output excel file.

Can you integrate Python into Excel?

PyXLL is very different to these other packages. Instead of just allowing you to read and write Excel files, PyXLL integrates Python into Excel. This allows you to run Python inside of Excel to extend Excel's capabilities with your own Python code!

How do I export large data from Python to excel?

Writing large data sets to Excel with Python and pandas.
Get the data from the API..
Munge the data..
Create the DataFrames..
Convert the ISO 8601 date strings..
Merge the DataFrames..
Clean up after the merge..
Write the data to Excel..
Code complete..