0% found this document useful (0 votes)
4 views3 pages

CSV Files in Python

The document provides a comprehensive guide on working with CSV files in Python, detailing their structure, usage, and how to manipulate them using the CSV module. It covers opening, reading, writing, filtering, and adding new columns to CSV files, providing code examples for each operation. CSV files are highlighted as essential for data analysis, spreadsheet applications, and managing data in various domains.

Uploaded by

ashutosh129.10
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

CSV Files in Python

The document provides a comprehensive guide on working with CSV files in Python, detailing their structure, usage, and how to manipulate them using the CSV module. It covers opening, reading, writing, filtering, and adding new columns to CSV files, providing code examples for each operation. CSV files are highlighted as essential for data analysis, spreadsheet applications, and managing data in various domains.

Uploaded by

ashutosh129.10
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

How to Work with CSV Files in Python

CSV, Comma Separated Values, is a plain text file format used to store structured tabular data. In a
CSV file, a line represents a row in a table. Each line consists of a comma-separated value,
representing a column in a row. A CSV file is saved with a .csv extension.
What are CSV Files Used For?
Data Analysis: CSV files are used to analyze data and to manipulate datasets for analysis.
Spreadsheet Applications: CSV files allow easy data manipulation and sharing using various
spreadsheet applications, such as Microsoft Excel and Google Sheets.
Customer Relationship Management (CRM): CSV files are used to manage and analyze data, such as
managing contact lists, campaign data, and customer management.
E-commerce and Inventory Management: CSV files help manage product listings and update
inventory to allow data to be coordinated in bulk.
Imagine you are a Data Analyst at a startup. You regularly receive data from many sources. To manage
the data, you start by storing it in a CSV file and processing it using a programming language such as
Python.
Getting Started
Before you start working with CSV files in Python, it’s crucial to set up your environment properly.
To work with CSV files in Python, we will use the CSV module part of the standard library (no need to
install it). We import the CSV module like so:
import csv
We can perform operations like adding a new column or filtering out the data using a Python
program.
How to Open CSV Files?
A CSV file can be opened using the built-in function open() with the appropriate mode like (‘r’ for
reading, ‘w’ for writing, or ‘a’ for appending). It’s better to use the with statement that automatically
handles the closing of the file even if any error occurs.
In a CSV file named [Link], we have the following data:
Name,Age,Department
Alice,30,HR
Bob,24,IT
Charlie,28,Finance
The following code opens the [Link] file using the open() function and with read mode.
Then we use the next() function to skip the header row and a for loop to iterate over each row in the
CSV file:
import csv
# Open the CSV file in read mode
with open('[Link]', mode='r') as file:
# Create a CSV reader object
csv_reader = [Link](file)
# Skip the header row (if there is one)
next(csv_reader, None)
# Iterate over each row in the CSV file
for row in csv_reader:
print(row)
How to Read CSV Files?
In a CSV file named [Link], we have the following information:
Name,Age,Department
Alice,30,HR
Bob,24,IT
Charlie,28,Finance
We write a Python script to read this CSV file and print its contents like so:
import csv
# Open the CSV file
with open('[Link]', mode= 'r') as file:
# Create a CSV reader object
csv_reader = [Link](file)
# Read the header
header = next(csv_reader)
print(f"Header: {header}")
# Read each row of the CSV file
for row in csv_reader:
print(f"Row: {row}")
In this code, we create a CSV reader object using [Link](file), which reads a CSV file and returns
each row as a list of strings. Next, we read the header of the CSV file using
the next(csv_reader) function which is used to retrieve the next item from an iterator. A header in a
CSV file is the first row that contains the names of the columns, providing a label for each column’s
data.
Then, we loop through the remaining rows in the CSV file, printing each row as a list of strings.
How to Write to a CSV File in Python?
To write the data into a CSV file, we first open the file in write mode using the with statement and
then create a writer object using the [Link](file) to allow us to write the data into the file.
In a CSV file named [Link], we have the following information:
Name,Age,Department
Alice,30,HR
Bob,24,IT
Charlie,28,Finance
Here’s the code snippet that opens the [Link] file and defines a list containing all the data that is
to be written into the CSV file.
Then, the [Link](file) object writes the data into the [Link] file by passing the list containing
the header row and all subsequent data rows as arguments to the writerows() function:
import csv
# Data to be written to CSV
data = [
["Name", "Age", "Department"],
["Alice", 30, "HR"],
["Bob", 24, "IT"],
["Charlie", 28, "Finance"]
]
# Open the CSV file in write mode
with open('[Link]', mode='w', newline='') as file:
# Create a CSV writer object
csv_writer = [Link](file)
# Write the rows to the CSV file
csv_writer.writerows(data)
How to Manipulate and Analyze Data using CSV Files?
Data manipulation and analysis are important skills to have for any data analyst or scientist.
The following are some data analysis things we can do with the CSV module in Python
Filtering Data
Filtering data involves selecting rows that meet certain criteria. For example, we might want to filter
rows where the age of the employees is greater than 25.
Here’s how we can filter the data for that:
filtered_data = [row for row in data if int(row['Age']) > 25]
# Display the filtered data
for row in filtered_data:
print(row)
Adding a New Column
Adding a new column involves creating additional data based on the existing columns or with new
data. For instance, we might want to add a column that calculates the age of each employee next
year.
Here’s how you can add a new column:
# Add a new column 'Age Next Year'
for row in data:
row['age_next_year'] = int(row['Age']) + 1
# Display the data with the new column
for row in data:
print(row)
This loop iterates over each row in the data list. In each row, it adds a new key-value pair where the
key is ‘age_next _year’ and the value is the current age incremented by one. This creates a new
column in the dataset.

You might also like