Home  >  Article  >  Backend Development  >  How to Extract Specific Columns from a CSV File Using Python?

How to Extract Specific Columns from a CSV File Using Python?

Susan Sarandon
Susan SarandonOriginal
2024-11-14 10:04:02206browse

How to Extract Specific Columns from a CSV File Using Python?

Trimming CSV Columns with the Python csv Module

Often, it is necessary to parse through a comma-separated values (CSV) file and extract specific columns of data. Using Python's csv module, one can accomplish this task with ease.

Column Extraction

To extract specific columns, utilize the included_cols variable, which specifies the column indices to be included. Each column is numbered starting from 0, with the first column being 0. For instance, to capture the "ID," "Name," "Zip," and "Phone" columns, you would write included_cols = [1, 2, 6, 7].

Iteration and Printing

To iterate through the rows and extract the specified columns, use a list comprehension within a for loop. For each row, the content variable will be populated with the values from the specified columns. To display the extracted data, use print(content) within the loop.

included_cols = [1, 2, 6, 7]

for row in reader:
    content = list(row[i] for i in included_cols)
    print(content)

Optimizing with Pandas

For more efficient CSV handling, consider using the pandas module. Pandas provides convenient and powerful tools for reading and manipulating CSV files. To read a CSV and store a specific column into a variable, simply use:

import pandas as pd
df = pd.read_csv(csv_file)
saved_column = df.column_name

The above is the detailed content of How to Extract Specific Columns from a CSV File Using Python?. 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