Home >Backend Development >Python Tutorial >How to Read Specific Columns from a CSV File Using the CSV Module and Pandas?

How to Read Specific Columns from a CSV File Using the CSV Module and Pandas?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-15 13:52:02309browse

How to Read Specific Columns from a CSV File Using the CSV Module and Pandas?

Read Specific Columns from a CSV File Using the CSV Module: A Comprehensive Guide

The desire to parse CSV files and extract data from specific columns is a common task in data analysis. To delve into this topic, let's consider an example CSV file:

ID | Name | Address | City | State | Zip | Phone | OPEID | IPEDS |
10 | C... | 130 W.. | Mo.. | AL... | 3.. | 334.. | 01023 | 10063 |

Suppose we need to capture only the columns containing ID, Name, Zip, and Phone.

Using the CSV Module

Initially, the approach was to iterate through each row using row[column_number]. However, this method proved ineffective. Instead, we can use the reader method of the CSV module and specify the columns we want:

import csv

included_cols = [1, 2, 6, 7]
with open(csv_file, 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter=' ')
    for row in reader:
        content = list(row[i] for i in included_cols)
        # Print the specific columns for each row
        print(content)

This code will print the desired columns for each row.

Introducing Pandas

While the above method is functional, the Pandas library offers a more elegant solution for working with CSV files. With Pandas, reading a CSV file and saving a specific column into a variable is straightforward:

import pandas as pd

# Read the CSV file into a DataFrame
df = pd.read_csv(csv_file)

# Save a specific column into a variable
names = df['Name']

Conclusion

To read specific columns from a CSV file using the CSV module, iterate through the rows and use list comprehension to extract the desired columns. For a more comprehensive solution, consider using the Pandas library, which provides an easy-to-use API for CSV file manipulation.

The above is the detailed content of How to Read Specific Columns from a CSV File Using the CSV Module and Pandas?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn