Rumah > pembangunan bahagian belakang > Tutorial Python > Bagaimana untuk Membaca Lajur Khusus daripada Fail CSV Menggunakan Modul CSV dan Panda?

Bagaimana untuk Membaca Lajur Khusus daripada Fail CSV Menggunakan Modul CSV dan Panda?

Patricia Arquette
Lepaskan: 2024-11-15 13:52:02
asal
313 orang telah melayarinya

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 |
Salin selepas log masuk

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)
Salin selepas log masuk

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']
Salin selepas log masuk

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.

Atas ialah kandungan terperinci Bagaimana untuk Membaca Lajur Khusus daripada Fail CSV Menggunakan Modul CSV dan Panda?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan