How to read data from excel file using Python Pandas
In this article, you will learn how to read data from excel file using Python Pandas.
Pandas is open source, fast, flexible, powerful and easy-to-use tool for data manipulation and data analysis, built on top of the Python programming language. It provides rich data structures and functions to make data analysis easy and fast. As we know, Microsoft Excel has widespread used in many different application fields. It is a helpful and powerful program for data analysis and documentation and rich in features like calculation, graphing tools, pivot tables, produce graphs and charts and much more.
The Python Pandas module provides an easy process to read excel file. First, we should have installed this module. So, open your command prompt and install using the following command -
pip install pandas
Suppose, we have the following excel file containing school program participants. With the help of this excel sheet, we will provide different programming examples to extract data.
Reading excel file using Python Pandas
Here, we have first imported the Pandas module and provided the excel sheet file with location in read_excel() method. This method reads the data into a Pandas dataframe. It accepts filename in the first parameter and sheet name in the second parameter. DataFrame is the key data structure of Pandas.
import pandas as pd df = pd.read_excel("school_event.xlsx") print(df)
The above code returns the following output -
Reading particular column in Excel
Instead of printing all excel records, we can also get specific data according to the requirements. Suppose, we want to select only the 'Name' column. In this case, specify the column name in pd.DataFrame as below -
import pandas as pd data = pd.read_excel("school_event.xlsx") df = pd.DataFrame(data, columns= ['Name']) print(df)
Reading more columns in Excel
In the above example, we have selected only one column, but we can also select more than one column. For this, specify the additional columns by separating their names with a comma. Here, we have retrieved the 'StudentId' and 'Program' columns -
import pandas as pd data = pd.read_excel("school_event.xlsx") df = pd.DataFrame(data, columns= ['StudentId', 'Program']) print(df)
Related ArticlesPython send mail to multiple recipients using SMTP server
How to generate QR Code in Python using PyQRCode
Python programs to check Palindrome strings and numbers
CRUD operations in Python using MYSQL Connector
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml