Skip to content

Latest commit

ย 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ“Š CSV Data Analyzer

A Python-based CSV Data Analyzer that reads employee data from a CSV file and generates useful statistics such as total records, average salary, department-wise employee counts, and the employee with the highest salary.

This project was built to practice CSV file handling, dictionaries, list processing, type conversion, lambda functions, max(), and basic data analysis using Python.


๐Ÿš€ Features

  • ๐Ÿ“‚ Read employee data from a CSV file
  • ๐Ÿ“Š Count total records
  • ๐Ÿ’ฐ Calculate average salary
  • ๐Ÿข Generate department-wise employee counts
  • ๐Ÿ† Find the employee with the highest salary
  • ๐Ÿ”„ Convert salary values from strings to integers
  • ๐Ÿง  Practice Python data processing concepts

๐Ÿ› ๏ธ Technologies Used

  • Python 3
  • csv module
  • Dictionaries
  • Lists
  • Functions
  • Classes
  • Lambda functions
  • sum()
  • max()
  • File handling

๐Ÿ“ Project Structure

python-csv-data-analyzer/
โ”‚
โ”œโ”€โ”€ main.py
โ”œโ”€โ”€ csv_analyzer.py
โ”œโ”€โ”€ employees.csv
โ”œโ”€โ”€ README.md
โ””โ”€โ”€ .gitignore

๐Ÿ“„ CSV Dataset

The project uses an employees.csv file.

Example:

Name,Department,Salary
Rahul,IT,50000
Aman,HR,42000
Priya,IT,65000
Neha,Sales,48000
Arjun,IT,58000
Simran,HR,45000
Riya,Sales,52000

๐Ÿง  How the Project Works

1. Import the CSV Module

Python provides the built-in csv module for working with CSV files.

import csv

2. Create the CSVAnalyzer Class

The CSVAnalyzer class manages the employee dataset and performs different calculations.

class CSVAnalyzer:
    def __init__(self, file_name):
        self.file_name = file_name
        self.employees = []

What happens here?

  • file_name stores the CSV file name.
  • employees stores all employee records.
  • Initially, employees is an empty list.

๐Ÿ“‚ Loading CSV Data

The load_data() method reads the CSV file.

def load_data(self):
    with open(self.file_name, "r", newline="") as file:
        reader = csv.DictReader(file)

        for row in reader:
            self.employees.append(row)

Why DictReader?

csv.DictReader() reads every row as a dictionary.

For example:

Name,Department,Salary
Rahul,IT,50000

becomes approximately:

{
    "Name": "Rahul",
    "Department": "IT",
    "Salary": "50000"
}

This makes accessing individual values easier:

employee["Name"]
employee["Department"]
employee["Salary"]

๐Ÿ’ฐ Calculating Average Salary

The get_average_salary() method calculates the average salary.

def get_average_salary(self):
    total_salary = sum(
        int(employee["Salary"])
        for employee in self.employees
    )

    return total_salary / len(self.employees)

Step-by-step

Suppose salaries are:

50000
42000
65000
48000
58000
45000
52000

First, convert salary values from strings to integers:

int(employee["Salary"])

Then calculate the total:

sum(...)

Finally:

Average = Total Salary / Number of Employees

๐Ÿข Department Summary

The get_department_summary() method counts employees in each department.

def get_department_summary(self):
    summary = {}

    for employee in self.employees:
        department = employee["Department"]

        if department not in summary:
            summary[department] = 0

        summary[department] += 1

    return summary

Example

Input:

Rahul  โ†’ IT
Aman   โ†’ HR
Priya  โ†’ IT
Neha   โ†’ Sales
Arjun  โ†’ IT
Simran โ†’ HR
Riya   โ†’ Sales

Output:

IT: 3
HR: 2
Sales: 2

Important Concept

This line:

summary[department] += 1

increases the count for that department.

For example:

summary["IT"] = 0

Then:

summary["IT"] += 1

becomes:

summary["IT"] = 1

and continues increasing whenever another IT employee is found.


๐Ÿ† Finding the Highest Salary

The get_highest_salary() method finds the employee with the highest salary.

def get_highest_salary(self):
    return max(
        self.employees,
        key=lambda employee: int(employee["Salary"])
    )

Understanding max()

Normally, max() finds the largest value.

For example:

numbers = [10, 50, 30]

print(max(numbers))

Output:

50

But here we have dictionaries instead of simple numbers.

Therefore, we tell max() what value it should compare:

key=lambda employee: int(employee["Salary"])

This means:

Compare employees based on their salary.

For the example dataset:

Priya โ†’ โ‚น65000

is the highest salary.


๐Ÿ“Š Example Output

๐Ÿ“Š CSV DATA ANALYZER

===== DATASET SUMMARY =====
Total Records : 7
Average Salary: โ‚น51428.57

===== DEPARTMENT SUMMARY =====
IT: 3 employees
HR: 2 employees
Sales: 2 employees

===== HIGHEST SALARY =====
Priya โ†’ โ‚น65000

๐Ÿงฉ Concepts Learned

1. CSV File Handling

Learned how to read structured data from .csv files using Python's built-in csv module.


2. csv.DictReader

Converts CSV rows into dictionaries using the column headers as keys.


3. Type Conversion

CSV data is generally read as strings.

For example:

"50000"

needs to be converted to:

50000

using:

int("50000")

This is necessary for mathematical operations.


4. Dictionary Counting

Used a dictionary to maintain department counts.

summary = {}

5. sum()

Used to calculate total salary.

sum(...)

6. max()

Used to find the employee with the highest salary.

max(...)

7. Lambda Functions

Used a lambda function to tell max() which value should be compared.

lambda employee: int(employee["Salary"])

8. List Processing

Employee records are stored inside a list:

self.employees = []

and processed one by one.


๐ŸŽฏ What I Practiced

Through this project, I practiced:

  • CSV file handling
  • csv.DictReader
  • Classes and objects
  • Lists
  • Dictionaries
  • Loops
  • String-to-integer conversion
  • sum()
  • max()
  • Lambda functions
  • Data aggregation
  • Basic dataset analysis
  • Reading structured data

๐Ÿ”ฎ Future Improvements

Possible improvements for future versions:

  • ๐Ÿ“ˆ Add salary charts
  • ๐Ÿ” Search employees
  • ๐Ÿข Filter data by department
  • ๐Ÿ“Š Add more statistical calculations
  • ๐Ÿ“ Allow users to select different CSV files
  • ๐Ÿ“ค Export analysis results
  • ๐Ÿ–ฅ๏ธ Build a Streamlit dashboard
  • ๐Ÿผ Add Pandas support
  • ๐Ÿ“Š Add data visualization using Matplotlib
  • ๐Ÿ—ƒ๏ธ Support larger datasets

๐Ÿ‘จโ€๐Ÿ’ป Author

Ayushman Tiwari

B.Tech Electronics & Communication Engineering Student

GitHub: ayushman-ece


โญ If you found this project useful, consider giving the repository a star!

About

A Python utility that analyzes CSV employee data, calculates salary statistics, summarizes departments, and identifies the highest-paid employee.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages