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!