How to read data from excel file using Python Pandas
In this article, you will learn how to read data from an excel file using the Python Pandas.
Pandas is an 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 been widely used in many different application fields. It is a helpful and powerful program for data analysis and documentation and is rich in features like calculation, graphing tools, pivot tables, producing graphs and charts, and much more.
Install Python Pandas
The Python Pandas module provides an easy way to read an excel file. First, we should have installed this module. So, open your command prompt and install it 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 the 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 ArticlesHow to read xml file in Python
Python 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
Python NumPy: Overview and Examples
Convert Python list to numpy array
numpy dot product
Python Pandas Plotting
Pandas string to datetime
Convert Excel to CSV Python Pandas
Python take screenshot of specific window
Read data from excel file using Python Pandas
Quick Introduction to Python Pandas
Python requests GET method
Python Convert XML to CSV
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list