Tutorial 2: Reading and Analyzing a Real CSV File Using Pandas

Introduction

In the previous chapter, we created data manually inside Python. Now we’ll work with a real CSV file — the most common format you’ll encounter in actual data analysis work, whether it’s exported from Excel, a database, or a web application.

By the end of this chapter, you’ll know how to:

  • Create a sample CSV file
  • Read a CSV file using Pandas
  • Explore a dataset
  • Analyze numerical data
  • Filter rows
  • Find maximum and minimum values
  • Create a visualization

1. What is a CSV File?

CSV stands for Comma-Separated Values — a plain text format where each line is a row, and commas separate the columns.

Name,Department,Salary,Experience
Amit,IT,75000,5
Priya,HR,60000,3
Rahul,Finance,85000,7

It looks like a simple text file, but it represents a table:

NameDepartmentSalaryExperience
AmitIT750005
PriyaHR600003
RahulFinance850007

CSV files commonly use the .csv extension — e.g., employees.csv. Its simplicity is exactly why it’s so widely used: virtually every spreadsheet tool, database, and analytics platform can export and import it.


2. Create a Sample CSV File

In JupyterLab, create a new code cell and run:

%%writefile employees.csv
Name,Department,Salary,Experience
Amit,IT,75000,5
Priya,HR,60000,3
Rahul,Finance,85000,7
Sneha,IT,90000,8
Vikram,Sales,65000,4
Anjali,Finance,95000,10

What does %%writefile do?
This is a Jupyter “magic command” — it tells the notebook to treat everything in that cell as file content, and writes it directly to a file (here, employees.csv) instead of running it as Python code. You should now see the file appear in the JupyterLab file browser.


3. Import Pandas

import pandas as pd

We use the alias pd, the standard short name for Pandas.


4. Read the CSV File

df = pd.read_csv("employees.csv")

This reads the CSV file and stores the data in a DataFrame called df — this single function is one of the most-used commands in all of data analysis, since almost every project starts by loading external data.

Display it:

df

Expected output:

NameDepartmentSalaryExperience
0AmitIT750005
1PriyaHR600003
2RahulFinance850007
3SnehaIT900008
4VikramSales650004
5AnjaliFinance9500010

🎉 You’ve successfully loaded a real CSV file!


5. Explore the Dataset

View the first rows:

df.head()

By default, head() shows the first five rows. You can specify a number:

df.head(3)

View the last rows:

df.tail()

Check the number of rows and columns:

df.shape

Output: (6, 4) → 6 rows, 4 columns.

View column names:

df.columns

Output:

Index(['Name', 'Department', 'Salary', 'Experience'], dtype='object')

Get information about the dataset:

df.info()

This tells you column names, number of values, data types, and memory usage — a fast health check on any newly-loaded dataset.

Get a statistical summary:

df.describe()

This provides count, mean, standard deviation, minimum, and maximum for numerical columns.


6. Select Columns

Select one column:

df["Salary"]

Select multiple columns (note the double square brackets — the outer one selects, the inner one is a list of column names):

df[["Name", "Salary"]]

Example with three columns:

df[["Name", "Department", "Salary"]]

7. Analyze Salary Data

Average salary:

df["Salary"].mean()

Highest salary:

df["Salary"].max()

Lowest salary:

df["Salary"].min()

Total salary:

df["Salary"].sum()

8. Find the Employee with the Highest Salary

df.loc[df["Salary"].idxmax()]

Expected output:

Name          Anjali
Department    Finance
Salary        95000
Experience    10

🎯 Insight: Anjali has the highest salary.


9. Filter Data

Find employees from the IT department:

df[df["Department"] == "IT"]

Find employees with salary greater than ₹80,000:

df[df["Salary"] > 80000]

Find employees with more than 5 years of experience:

df[df["Experience"] > 5]

(In each case, the condition inside the brackets creates a True/False value for every row, and Pandas keeps only the rows marked True — this pattern is called boolean filtering and is one of the most fundamental tools in data analysis.)


10. Multiple Conditions

Find employees who work in Finance and have a salary greater than ₹90,000:

df[
    (df["Department"] == "Finance") &
    (df["Salary"] > 90000)
]

Important operators:

  • & → AND
  • | → OR

(Note: each condition must be wrapped in its own parentheses when combining them — this is a common gotcha in Pandas.)

Example using OR:

df[
    (df["Department"] == "IT") |
    (df["Department"] == "Finance")
]

11. Group Data

Let’s calculate the average salary by department:

df.groupby("Department")["Salary"].mean()

Possible output:

Department
Finance    90000
HR         60000
IT         82500
Sales      65000

This is an important data analysis pattern: Group → Calculate → Compare. (We’ll dive much deeper into this in Chapter 8.)


12. Create a Visualization

Let’s visualize the average salary by department:

import matplotlib.pyplot as plt

average_salary = df.groupby("Department")["Salary"].mean()

average_salary.plot(kind="bar")
plt.title("Average Salary by Department")
plt.xlabel("Department")
plt.ylabel("Average Salary")
plt.show()

This allows us to visually compare departments — notice that Pandas Series objects (like average_salary here) have a built-in .plot() method, which internally uses Matplotlib.


Practice Exercise

Modify the dataset by adding: Age, City, Performance_Score. Then answer:

  1. Who is the oldest employee?
  2. What is the average performance score?
  3. Which department has the highest average salary?
  4. Which employees have a salary above ₹80,000?
  5. Create a chart showing the number of employees in each department.

Scroll to Top