Reading excel to a pandas dataframe starting from row 5 and including headers

Given an excel file, we have to read it to a pandas dataframe starting from row 5 and including headers. By Pranit Sharma Last updated : October 03, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Read an excel file in the form of pandas DataFrame starting from row 5 and including headers

An xlsx file is a spreadsheet file that can be created with certain software, especially with the popularly known MS Excel. To import an Excel file, we use the following piece of code:

pd.ExcelFile('Path_of_the_file');

Here, we will use the same method as mentioned above but we will pass a parameter called skiprows for which we will assign a list of rows to be excluded.

Python program to read excel to a pandas dataframe starting from row 5 and including headers

# Importing pandas package
import pandas as pd

# Importing Excel file
file = pd.ExcelFile('D:/Book1.xlsx')
df = file.parse('B', skiprows=4)

# Display DataFrame
print("DataFrame:\n",df)

Output

The output of the above program is:

Example: Reading excel to a pandas dataframe starting from row 5 and including headers

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.