
Exploring CSV in Python
CSV stands for Comma-Separated Values. It's a simple and widely used file format for storing tabular data, such as spreadsheets and databases. In CSV files, each line represents a row of data, and commas (or other delimiters) separate the values in each row.
In Python, you can easily read CSV files using the csv module. First, you need to open the CSV file using the open() function, and then you can use the csv.reader object to read the file line by line.
# Open the CSV file
with open('data.csv', 'r') as file:
# Create a CSV reader object
reader = csv.reader(file)
# Iterate over each row in the CSV file
for row in reader:
print(row)
Similarly, you can write data to CSV files using the csv.writer object. First, you need to open the CSV file in write mode ('w'), and then you can use the writer.writerow() method to write each row of data to the file.
# Open a new CSV file in write mode
with open('output.csv', 'w', newline='') as file:
# Create a CSV writer object
writer = csv.writer(file)
# Write rows to the CSV file
writer.writerow(['Name', 'Age', 'City'])
writer.writerow(['Alice', '30', 'New York'])
writer.writerow(['Bob', '25', 'Los Angeles'])
Once you've read data from a CSV file, you can process it in various ways. For example, you can perform calculations, filter rows based on certain conditions, or create visualizations using libraries like pandas and matplotlib.
# Open the CSV file
with open('data.csv', 'r') as file:
# Create a CSV reader object
reader = csv.reader(file)
ages = []
# Iterate over each row in the CSV file
for row in reader:
age = int(row[1]) # Assuming age is in the second column
ages.append(age)
# Calculate the average age
average_age = sum(ages) / len(ages)
print("Average Age:", average_age)
1. Sales Data Analysis:
Scenario: A retail store wants to analyze its sales data to identify trends
and patterns. They have a CSV file containing sales records for different
products over a period of time.
# Open the CSV file
with open('sales_data.csv', 'r') as file:
# Create a CSV reader object
reader = csv.reader(file)
# Initialize variables for total sales and number of transactions
total_sales = 0
num_transactions = 0
# Iterate over each row in the CSV file
for row in reader:
# Assuming sales amount is in the second column
sales_amount = float(row[1])
total_sales += sales_amount
num_transactions += 1
# Calculate average sales per transaction
average_sales = total_sales / num_transactions
print("Average Sales per Transaction:", average_sales)
2. Student Grades Report:
Scenario: A school wants to generate a report of student grades from a CSV
file containing student information and grades for different subjects.
# Open the CSV file
with open('student_grades.csv', 'r') as file:
# Create a CSV reader object
reader = csv.DictReader(file)
# Initialize variables for total grades and number of students
total_grades = 0
num_students = 0
# Iterate over each row in the CSV file
for row in reader:
# Assuming grades are stored as integers in separate columns
math_grade = int(row['Math'])
science_grade = int(row['Science'])
english_grade = int(row['English'])
# Calculate total grade for each student
total_grade = math_grade + science_grade + english_grade
total_grades += total_grade
num_students += 1
# Calculate average grade per student
average_grade = total_grades / num_students
print("Average Grade per Student:", average_grade)
3. Inventory Management:
Scenario: A warehouse needs to manage its inventory of products stored in
CSV format. They want to track the quantity of each product and generate a
report.
# Open the CSV file
with open('inventory.csv', 'r') as file:
# Create a CSV reader object
reader = csv.DictReader(file)
# Initialize a dictionary to store product quantities
product_quantities = {}
# Iterate over each row in the CSV file
for row in reader:
product = row['Product']
quantity = int(row['Quantity'])
# Update product quantities
if product in product_quantities:
product_quantities[product] += quantity
else:
product_quantities[product] = quantity
# Print product quantities
for product, quantity in product_quantities.items():
print(product, ":", quantity)
4. Customer Feedback Analysis :
Scenario: A company collects customer feedback in a CSV file and wants to
analyze it to identify common themes and issues mentioned by customers.
# Open the CSV file
with open('customer_feedback.csv', 'r') as file:
# Create a CSV reader object
reader = csv.DictReader(file)
# Initialize a dictionary to store feedback categories and counts
feedback_counts = {}
# Iterate over each row in the CSV file
for row in reader:
feedback = row['Feedback']
# Update feedback counts
if feedback in feedback_counts:
feedback_counts[feedback] += 1
else:
feedback_counts[feedback] = 1
# Print feedback categories and counts
for feedback, count in feedback_counts.items():
print(feedback, ":", count)
In summary, CSV (Comma-Separated Values) is a simple file format for storing tabular data. Python provides the csv module, which allows you to easily read and write CSV files. By using this module, you can manipulate CSV data and perform various operations on it, making it a versatile tool for handling tabular data in Python.