Doc from O'Reilly AI Assist
=============================

Python 3 Reads CSV File
There are two primary ways to read a CSV file in Python 3: using the built-in csv module and using the popular pandas library.

Method 1: Using the csv Module
Import the csv module: import csv
Open the CSV file in read mode: with open('example.csv', 'r') as csvfile:
Create a csv.reader object: readCSV = csv.reader(csvfile, delimiter=',')
Iterate over the rows: for row in readCSV:
Access individual columns using indexing (e.g., row[0] for the first column)
Example code:

import csv

with open('example.csv', 'r') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        print(row)  # prints each row as a list
        print(row[0])  # prints the first column
        print(row[0], row[1], row[2])  # prints specific columns

Method 2: Using pandas
Import the pandas library: import pandas as pd
Read the CSV file into a DataFrame object: df = pd.read_csv('example.csv')
Access individual columns using the column name (e.g., df['column_name']) or indexing (e.g., df.iloc[:, 0] for the first column)
Example code:

import pandas as pd

df = pd.read_csv('example.csv')
print(df)  # prints the entire DataFrame
print(df['column_name'])  # prints a specific column
print(df.iloc[:, 0])  # prints the first column

Key differences
The csv module is more lightweight and flexible, but requires more manual effort to parse the CSV file.
pandas provides a more convenient and powerful way to work with structured data, but has a larger overhead and requires more memory.
Choose the method that best fits your specific use case and requirements.
