python
  1. python-read-excel-file

Python Read Excel File

Syntax

import pandas as pd

# Read Excel file
data = pd.read_excel('file_name.xlsx', sheet_name='Sheet1')

Example

import pandas as pd

# Read Excel file
data = pd.read_excel('example.xlsx', sheet_name='Sheet1')

# Print dataframe
print(data)

Output

   ID     Name
0   1     John
1   2     Jane
2   3     Bob

Explanation

In order to read an Excel file in Python, we need to use the pandas library. Pandas is a powerful and easy-to-use library for data manipulation and analysis. We can use the read_excel() function provided by pandas to read an Excel file.

We need to pass the file_name and sheet_name parameters to the read_excel() function. file_name parameter is the name of the Excel file that we want to read and sheet_name parameter is the name of the sheet that we want to read from the Excel file.

The read_excel() function returns a dataframe which contains the data from the Excel file.

Use

We can use this operation when we want to read the data from an Excel file and perform some operations or analysis on it in Python. This is a very common operation when working with data in Python, as Excel is a widely used format for storing and sharing data.

Important Points

  • Make sure that the Excel file is present in the current working directory or provide the full path of the file.
  • Pandas library needs to be installed before we can use it in our code.

Summary

In this tutorial, we learned how to read an Excel file in Python using pandas library. We learned about the syntax, example, output, explanation, use, important points and summary of this operation.

Published on: