Maison >développement back-end >Tutoriel Python >Comment lire des colonnes spécifiques à partir d'un fichier CSV à l'aide du module CSV et de Pandas ?

Comment lire des colonnes spécifiques à partir d'un fichier CSV à l'aide du module CSV et de Pandas ?

Patricia Arquette
Patricia Arquetteoriginal
2024-11-15 13:52:02297parcourir

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.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn