How to Read Specific Columns from Excel File?

Learn, how to read specific columns from excel file in Python? By Pranit Sharma Last updated : October 06, 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.

Problem statement

Suppose that we are reading an excel file using the pandas.read_excel() method and we need to read some specific columns of this file.

Reading Specific Columns from Excel File

For this purpose, we will still use pandas.read_excel() method but this time we need to pass an argument called usecols.

  • It is a string-like or list-like argument.
  • If it is None, then pandas.read_excel() will parse all columns.
  • If str, then indicates comma separate list of Excel column letters and column ranges (e.g. "A:E" or "A,C,E:F"). Ranges are inclusive of both sides.
  • If the list of int, then indicates the list of column numbers to be parsed (0-indexed).
  • If the list of strings, then indicates the list of column names to be parsed.

Let us understand with the help of an example,

Python program to read specific columns from excel file

# Importing pandas package
import pandas as pd

# Reading an excel file with certain columns
data = pd.read_excel("excel.xlsx",usecols="C:H")

# Creating a dataframe with the data set
df = pd.DataFrame(data)

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

Output

The output of the above program is:

Example: How to Read Specific Columns from Excel File?

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





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