The Wayback Machine - https://web.archive.org/web/20240204103946/https://www.geeksforgeeks.org/reading-specific-columns-of-a-csv-file-using-pandas/
Open In App
Related Articles

Reading specific columns of a CSV file using Pandas

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

CSV files are widely utilized for storing tabular data in file systems, and there are instances where these files contain extraneous columns that are irrelevant to our analysis. This article will explore techniques for selectively reading specific columns from a CSV file using Python.

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 the keyword usecols. It will return the data of the CSV file of specific columns.

Read Specific Columns From CSV File

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.

Link of the CSV file used: link

Python3




# importing the module
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 read_csv()

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.

Link of the CSV file used: link

Python3




# importing the module
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 

Read Specific Columns of a CSV File Using usecols

In this example, the Pandas library is imported, and the code reads the ‘Hours’, ‘Scores’, and ‘Pass’ columns from the “student.csv” file using Pandas. The resulting DataFrame ‘df’ displays the selected columns for further analysis.

Link of the CSV file used: link

Python3




# importing the module
import pandas as pd
 
# read specific columns of csv file using Pandas
df = pd.read_csv("student.csv", usecols = ['Hours','Scores','Pass'])
print(df)


Output

In this example, the Pandas library is imported, and 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. Link of the CSV file used: link

Python3




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


Output


Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.

Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.


Last Updated : 03 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials