• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Read Specific Columns From CSV File

Read Specific Columns From CSV File

Author: Aditya Raj
Last Updated: September 19, 2022

CSV files are the most popular way to store tabular data in the file system. Sometimes the csv file can contain multiple columns that we don’t need for analysis. In this article, we will discuss how we can read specific columns from a csv file in python.

Read Specific Columns From CSV File Using Pandas Dataframe

To read a csv file in python, we use the read_csv() method provided in the pandas module. The read_csv() method takes the name of the csv file as its input argument. After execution, the read_csv() method returns the dataframe containing the data of the csv file. You can observe this in the following example.

import pandas as pd
import numpy as np
df=pd.read_csv("demo_file.csv")
print("The dataframe is:")
print(df)

Output:

The dataframe is:
     Name  Roll    Language
0  Aditya     1      Python
1     Sam     2        Java
2   Chris     3         C++
3    Joel     4  TypeScript

As you can see, the read_csv() method returns the dataframe with all the columns of the csv file. To read a specific column from the dataframe, we can use the column name as an index as we do to obtain an element from a list. For this, we can simply pass the column name to the square bracket after the dataframe as shown in the example.

import pandas as pd
import numpy as np
df=pd.read_csv("demo_file.csv")
print("The dataframe is:")
print(df)
specific_column=df["Name"]
print("The column is:")
print(specific_column)

Output:

The dataframe is:
     Name  Roll    Language
0  Aditya     1      Python
1     Sam     2        Java
2   Chris     3         C++
3    Joel     4  TypeScript
The column is:
0    Aditya
1       Sam
2     Chris
3      Joel
Name: Name, dtype: object

To read multiple columns from the dataframe, we can pass a list of column names in the square brackets as shown below.

import pandas as pd
import numpy as np
df=pd.read_csv("demo_file.csv")
print("The dataframe is:")
print(df)
specific_columns=df[["Name","Roll"]]
print("The column are:")
print(specific_columns)

Output:

The dataframe is:
     Name  Roll    Language
0  Aditya     1      Python
1     Sam     2        Java
2   Chris     3         C++
3    Joel     4  TypeScript
The column are:
     Name  Roll
0  Aditya     1
1     Sam     2
2   Chris     3
3    Joel     4

Suggested Reading: If you are into machine learning, you can read this article on regression in machine learning. You might also like this article on k-means clustering with numerical example.

Read Specific Columns From CSV File Using The ‘usecols’ Parameter

Reading the entire dataframe and extracting columns from the dataframe doesn’t serve our purpose. In the above approach, we are also reading the unwanted columns into the dataframe. After that, we are reading the specific columns. We can avoid this entire process by using the ‘usecols’ parameter in the read_csv() method. To read specific columns from the csv file, we will pass the list of columns to be read as an input argument to the ‘usecols’ parameter. After execution, the read_csv() method returns the dataframe with specific columns as shown in the following example.

import pandas as pd
import numpy as np
df=pd.read_csv("demo_file.csv",usecols=["Name"])
print("The dataframe is:")
print(df)

Output:

The dataframe is:
     Name
0  Aditya
1     Sam
2   Chris
3    Joel

As you can see above, we have read only the specific columns from the csv file using the ‘usecols’ parameter.

Conclusion

In this article, we have discussed how to read specific columns from a csv file. To know more about python programming, you can read this article on dictionary comprehension in python. You might also like this article on list comprehension on python.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehensions in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Pandas Append Row to DataFrame
  • Convert String to DataFrame in Python
  • Pandas DataFrame to List in Python
  • Solved: Dataframe Constructor Not Properly Called Error in Pandas
  • Overwrite a File in Python

Copyright © 2012–2023 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us