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

Day,Temperature,Rainfall
1, 30, 5
2, 32, 0
3, 28, 10
4, 35, 2

We must:

  1. Read the CSV file

  2. Convert rows into dictionary format like:

{
 'Temperature': [30,32,28,35],
 'Rainfall': [5,0,10,2]
}

  1. Ask user:

    • Which column?

    • What operation? (max/min/avg)

  2. Perform the requested calculation

  3. Display result


👉 Key Idea:

  • CSV → Dictionary

  • Dictionary values → List

  • Perform aggregation


Flowchart (Block Diagram of Logic)

Flowchart Explanation
  • 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

  1. Start

  2. Open CSV file

  3. Read first line (header)

  4. Create dictionary with column names

  5. For each row:

    • Convert values to float

    • Append to respective list

  6. Ask user for column name

  7. Ask user for operation

  8. If operation = max → display max

  9. If operation = min → display min

  10. If operation = avg → display sum/len

  11. 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

Enter CSV file name: data.csv
Available columns: ['Day', 'Temperature', 'Rainfall']
Enter column name: Temperature
Enter operation (max/min/avg): max
Maximum = 35.0

Case 2

Enter column name: Rainfall
Enter operation (max/min/avg): avg
Average = 4.25

Important Concepts Used

  • CSV module

  • csv.DictReader

  • Dictionary of lists

  • Type conversion

  • Built-in functions (max()min()sum()len())

  • Conditional statements


Viva Voce Questions

  1. What is CSV file?

  2. What does DictReader do?

  3. Why convert values to float?

  4. What happens if column name is wrong?

  5. Difference between read() and DictReader?

  6. Can this handle non-numeric columns?

  7. What is dictionary of lists?



Procedure to Execute the Program

  1. Create CSV file (example: data.csv)

  2. Add sample numeric data

  3. Save in same folder

  4. Run Python program

  5. Enter file name

  6. 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:

Left Side of the Record
3. Flowchart
4. Algorithm
5. Output
⚠️ Students must write ALL possible outputs.
Write the following neatly on the left-hand side page:

-:END:-


Comments

Popular posts from this blog