Program10
10. Develop Data Summary Generator: Read a CSV file (like COVID data or weather stats), convert to dictionary form, and allow the user to run summary queries: max, min, average by column.
Objective of the Program
To understand CSV file handling
To convert CSV rows into dictionary format
To perform column-wise statistical operations
To build an interactive data query system
Logical Construction (Think Before You Code)
Suppose we have a CSV file:
Example: data.csv
We must:
Read the CSV file
Convert rows into dictionary format like:
Ask user:
Which column?
What operation? (max/min/avg)
Perform the requested calculation
Display result
👉 Key Idea:
CSV → Dictionary
Dictionary values → List
Perform aggregation
Flowchart (Block Diagram of Logic)
Start
Open CSV file
Read header
Store column data in dictionary
Ask user for column name
Ask for operation
Perform calculation
Display result
Stop
Algorithm
Start
Open CSV file
Read first line (header)
Create dictionary with column names
For each row:
Convert values to float
Append to respective list
Ask user for column name
Ask user for operation
If operation = max → display max
If operation = min → display min
If operation = avg → display sum/len
Stop
Python Program
⚠ Students must complete missing parts.
# Data Summary Generator
import csv
filename = input("Enter CSV file name: ")
data = {}
with open(filename, "r") as file:
reader = csv.DictReader(file)
for row in reader:
for key in row:
if key not in data:
data[key] = []
data[key].append(__________(row[key]))
print("Available columns:", list(data.keys()))
column = input("Enter column name: ")
operation = input("Enter operation (max/min/avg): ")
if column in data:
if operation == "max":
print("Maximum =", ____________(data[column]))
elif operation == "min":
print("Minimum =", ____________(data[column]))
elif operation == "avg":
avg = ____________(data[column]) / ____________(data[column])
print("Average =", avg)
else:
print("Invalid operation")
else:
print("Invalid column name")
Probable Output
Assume data.csv:
Day,Temperature,Rainfall
1,30,5
2,32,0
3,28,10
4,35,2
Case 1
Case 2
Important Concepts Used
CSV module
csv.DictReaderDictionary of lists
Type conversion
Built-in functions (
max(),min(),sum(),len())Conditional statements
Viva Voce Questions
What is CSV file?
What does
DictReaderdo?Why convert values to float?
What happens if column name is wrong?
Difference between read() and DictReader?
Can this handle non-numeric columns?
What is dictionary of lists?
Procedure to Execute the Program
Create CSV file (example:
data.csv)Add sample numeric data
Save in same folder
Run Python program
Enter file name
Enter column and operation
Google Colab – Online Execution
👉 Click the link below to run the complete working version of this program online:
[ https://colab.research.google.com/drive/1LQ8ZvBwzkv5dZduGu6XBzFTZk146YgG4?usp=sharing ]
Program Explanation
Assignment for Students
Add median calculation
Add continuous loop for multiple queries
Handle non-numeric columns
Sort results before displaying
Lab Record Instructions
Right Side of the Record
1. Problem Statement
2. Python Program
Write the following neatly on the right-hand side page:
3. Flowchart
4. Algorithm
5. Output
⚠️ Students must write ALL possible outputs.
Write the following neatly on the left-hand side page:

Comments
Post a Comment