Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data preprocessing
  • Data Manipulation
  • Data Analysis using Pandas
  • EDA
  • Pandas Exercise
  • Pandas AI
  • Numpy
  • Matplotlib
  • Plotly
  • Data Analysis
  • Machine Learning
  • Data science
Open In App
Next Article:
How to skip rows while reading csv file using Pandas?
Next article icon

Reading specific columns of a CSV file using Pandas

Last Updated : 21 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

When working with large datasets stored in CSV (Comma-Separated Values) files, it’s often unnecessary to load the entire dataset into memory. Instead, you can selectively read specific columns using Pandas in Python.

Read Specific Columns From CSV File

Let us see how to read specific columns of a CSV file using Pandas. This can be done with the help of the pandas.read_csv() method. We will pass the first parameter as the CSV file and the second parameter as the list of specific columns in keyword usecols. It will return the data of the CSV file of specific columns.

The usecols parameter in the read_csv() function filters the columns to be loaded into the DataFrame. This is particularly useful when:

  • The dataset contains hundreds or thousands of columns.
  • You are only interested in analyzing a subset of the data.

Below are some examples by which we can read specific columns of a CSV file using Pandas.

Read Entire Columns of a CSV File

In this example, the Pandas library is imported, and the code reads the entire content of the “student_scores2.csv” file into a DataFrame ‘df’ using Pandas. The printed output displays the entire dataset for further examination.

Python
import pandas as pd  # read specific columns of csv file using Pandas df = pd.read_csv("student_scores2.csv") print(df) 

Output:

Screenshot-2023-11-24-132727

Read Specific Columns of a CSV File Using usecols

In this example, the Pandas library is imported, and the code uses it to read only the ‘IQ’ and ‘Scores’ columns from the “student_scores2.csv” file, storing the result in the DataFrame ‘df’. The printed output displays the selected columns for analysis.

Python
import pandas as pd  # read specific columns of csv file using Pandas df = pd.read_csv("student_scores2.csv", usecols=['IQ', 'Scores']) print(df) 

Output:

With another example, the code reads the ‘Survived’ and ‘Pclass’ columns from the “titanic.csv” file using Pandas. The resulting DataFrame ‘df’ displays the selected columns for analysis.

Python
import pandas as pd   # read specific columns of csv file using Pandas df = pd.read_csv("titanic.csv", usecols = ['Survived','Pclass']) print(df) 

Output:

Selecting Specific Columns by Index

If you don’t know the column names or prefer working with indices, you can pass a list of integers representing column positions:

Python
# Read only the thid and last column (indices 2 and 3) df = pd.read_csv('student_scores2.csv', usecols=[2, 3]) df 

Output:

	Scores	Pass
0 18 0
1 45 1
2 25 0
3 72 1
4 30 0
5 20 0
6 88 1
..
..

Using Lambda for Dynamic Selection

You can also use functions or regular expressions to dynamically select columns. For example:

Python
# Dynamically select columns containing "Name" or "Salary" df = pd.read_csv('/content/student_scores2.csv', usecols=lambda col: 'IQ' in col or 'Pass' in col) df 

Output:

	IQ	Pass
0 80 0
1 80 1
2 70 0
3 90 1
4 70 0
5 80 0
6 100 1
7 90 1
..


Next Article
How to skip rows while reading csv file using Pandas?
author
vipinyadav15799
Improve
Article Tags :
  • AI-ML-DS
  • Pandas
  • AI-ML-DS With Python
  • Python-pandas

Similar Reads

  • How to import excel file and find a specific column using Pandas?
    To read specific columns from an Excel file in Pandas, you have the flexibility to use either column indices or letters. This is achieved by setting the usecols argument, which can take a comma-separated string or a list containing column identifying letters or indices. In this article, we will lear
    5 min read
  • How to skip rows while reading csv file using Pandas?
    Python is a good language for doing data analysis because of the amazing ecosystem of data-centric python packages. Pandas package is one of them and makes importing and analyzing data so much easier. Here, we will discuss how to skip rows while reading csv file. We will use read_csv() method of Pan
    3 min read
  • Replacing column value of a CSV file in Python
    Let us see how we can replace the column value of a CSV file in Python. CSV file is nothing but a comma-delimited file. Method 1: Using Native Python way  Using replace() method, we can replace easily a text into another text.  In the below code, let us have an input CSV file as "csvfile.csv" and be
    2 min read
  • Convert Text File to CSV using Python Pandas
    Converting Text File to CSV using Python Pandas refers to the process of transforming a plain text file (often with data separated by spaces, tabs, or other delimiters) into a structured CSV (Comma Separated Values) file using the Python Pandas library. In this article we will walk you through multi
    2 min read
  • Export Specific Columns in DataFrame to CSV File
    A data frame is a tabular data structure similar to a spreadsheet. It has rows and columns. CSV (Comma-Separated Values) is a plain text file format used for storing tabular data. Each line in a CSV file represents a row of data, and the values within a line are separated by commas. This article exp
    3 min read
  • Rename column name with an index number of the CSV file in Pandas
    In this blog post, we will learn how to rename the column name with an index number of the CSV file in Pandas. Renaming Column Name with an Index NumberPandas, an advanced data manipulation package in Python, includes several methods for working with structured data such as CSV files. You might wish
    6 min read
  • Select Columns with Specific Data Types in Pandas Dataframe
    In this article, we will see how to select columns with specific data types from a dataframe. This operation can be performed using the DataFrame.select_dtypes() method in pandas module. Syntax: DataFrame.select_dtypes(include=None, exclude=None)Parameters : include, exclude : A selection of dtypes
    2 min read
  • Get a List of a Specific Column of a Pandas DataFrame
    In data analysis, extracting specific columns from a DataFrame and converting them into Python lists is a common requirement. Pandas provides multiple ways to achieve this efficiently. This article explores various methods to extract a specific column from a Pandas DataFrame and convert it into a li
    3 min read
  • How to Select Single Column of a Pandas Dataframe
    In Pandas, a DataFrame is like a table with rows and columns. Sometimes, we need to extract a single column to analyze or modify specific data. This helps in tasks like filtering, calculations or visualizations. When we select a column, it becomes a Pandas Series, a one-dimensional data structure th
    2 min read
  • How to Convert Pandas Columns to String
    Converting columns to strings allows easier manipulation when performing string operations such as pattern matching, formatting or concatenation. Pandas provides multiple ways to achieve this conversion and choosing the best method can depend on factors like the size of your dataset and the specific
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences