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.
- ๐ 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
- Python 3
csvmodule- Dictionaries
- Lists
- Functions
- Classes
- Lambda functions
sum()max()- File handling
python-csv-data-analyzer/
โ
โโโ main.py
โโโ csv_analyzer.py
โโโ employees.csv
โโโ README.md
โโโ .gitignore
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,52000Python provides the built-in csv module for working with CSV files.
import csvThe CSVAnalyzer class manages the employee dataset and performs different calculations.
class CSVAnalyzer:
def __init__(self, file_name):
self.file_name = file_name
self.employees = []file_namestores the CSV file name.employeesstores all employee records.- Initially,
employeesis an empty list.
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)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"]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)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
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 summaryInput:
Rahul โ IT
Aman โ HR
Priya โ IT
Neha โ Sales
Arjun โ IT
Simran โ HR
Riya โ Sales
Output:
IT: 3
HR: 2
Sales: 2
This line:
summary[department] += 1increases the count for that department.
For example:
summary["IT"] = 0Then:
summary["IT"] += 1becomes:
summary["IT"] = 1and continues increasing whenever another IT employee is found.
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"])
)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.
๐ 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
Learned how to read structured data from .csv files using Python's built-in csv module.
Converts CSV rows into dictionaries using the column headers as keys.
CSV data is generally read as strings.
For example:
"50000"needs to be converted to:
50000using:
int("50000")This is necessary for mathematical operations.
Used a dictionary to maintain department counts.
summary = {}Used to calculate total salary.
sum(...)Used to find the employee with the highest salary.
max(...)Used a lambda function to tell max() which value should be compared.
lambda employee: int(employee["Salary"])Employee records are stored inside a list:
self.employees = []and processed one by one.
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
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
Ayushman Tiwari
B.Tech Electronics & Communication Engineering Student
GitHub: ayushman-ece
โญ If you found this project useful, consider giving the repository a star!