首頁 >後端開發 >Python教學 >如何使用 CSV 模組和 Pandas 從 CSV 檔案讀取特定列?

如何使用 CSV 模組和 Pandas 從 CSV 檔案讀取特定列?

Patricia Arquette
Patricia Arquette原創
2024-11-15 13:52:02309瀏覽

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.

以上是如何使用 CSV 模組和 Pandas 從 CSV 檔案讀取特定列?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn