Reading specific columns from a CSV file can be a common task in data processing. However, accessing columns by their number, as attempted in the given code, may lead to unexpected results.
The mistake in the provided code lies in placing the print statement outside the for loop. Consequently, only the last iteration's result is printed, which is the last column of the CSV file.
To fix this issue, the print statement should be moved inside the loop to print each row's specific columns:
for row in reader: content = list(row[i] for i in included_cols) print(content)
While the adjusted code can accomplish the task, it lacks the convenience and efficiency of the pandas module. Pandas provides an elegant and versatile solution for CSV file manipulation.
Using pandas, reading a specific column from a CSV file becomes effortless:
import pandas as pd df = pd.read_csv(csv_file) # Save column data to a variable column_data = df['column_name']
The Pandas solution significantly simplifies the code:
import pandas as pd df = pd.read_csv(csv_file) # Save specific columns as a list specific_columns = ['ID', 'Name', 'Zip', 'Phone'] data = df[specific_columns].values.tolist()
The above is the detailed content of How to Read Specific Columns from a CSV File Efficiently?. For more information, please follow other related articles on the PHP Chinese website!