Current Level 1: Pandas Level - 0

Mile 1: What Is Pandas and Why We Use It

🎬 YouTube Videos

No YouTube videos available

Check back later for new YouTube content.

📹 Library Videos

No library videos available

Sign up free to watch library videos on this mile, or explore the first three miles on free courses.

📚 Content

SECTION 1 DATA AND TABLES - THE PROBLEM PANDAS SOLVES

3 min read

Data is simply recorded facts: names, marks, prices, dates, temperatures. When facts are organised into rows and columns they form a table. Each row is one record, such as one student. Each column is one kind of fact, such as marks.

You already know tables: an Excel sheet, a school mark list, a bank statement. The first row usually holds column names (the header) and the rest are records.

Python alone can hold a table using lists, but it has no real idea of rows, columns or names. If you want the average of the marks column you must write a loop yourself. Pandas gives Python a proper idea of tables.

header = ["name", "marks"]
rows = [["Asha", 80], ["Ravi", 90], ["Meena", 75]]

print(header)
for row in rows:
    print(row)

total = 0
for row in rows:
    total = total + row[1]
print("Average:", round(total / len(rows), 2))

Code explained:

Code What it does
header = [...] A list holding the column names
rows = [[...], ...] A list of lists; each inner list is one record
for row in rows Repeats for every record
row[1] The second item (marks) of that record
total / len(rows) Total divided by number of records gives the average

Output explained:

Output Meaning
First line The header
Next three lines One record each
Average: 81.67 (80 + 90 + 75) / 3

Watch out: This works, but notice how much code a simple average needed and that we had to remember that marks is position 1. Pandas removes this pain.
Quick check: In a table, what is a row and what is a column?
Answer: A row is one record; a column is one kind of fact for all records.

Sign up or sign in to rate this section
SECTION 2 WHAT IS PANDAS?

3 min read

Pandas is a free, open-source Python library for working with table-style data. It was created by Wes McKinney in 2008 and is now one of the most used libraries in the world.

The name comes from Panel Data, a term used in statistics for tables of observations. It is not related to the animal. The name is always written Pandas in text, and pandas in code.

Pandas is built on another library called NumPy, which makes number calculations fast. Pandas adds names for rows and columns, mixed data types in one table, and tools to read, clean, group and save data.

Pandas is a third-party library, so it is not included in Python. You install it once (Mile 4) and import it in every program (Mile 6). Here we only check that it is available and print its version.

import pandas as pd

print("Pandas version:", pd.__version__)
print("Library name  :", pd.__name__)
print(type(pd))

Code explained:

Code What it does
import pandas as pd Loads Pandas and nicknames it pd (full explanation in Mile 6)
pd.version A stored text showing the installed version
pd.name The official name of the library
type(pd) Shows pd is a module, which is Python's word for a library file

Output explained:

Output Meaning
Pandas version Your installed version number
pandas The real library name
class 'module' pd is a library loaded into the program

Watch out: If this code gives ModuleNotFoundError, Pandas is not installed yet. Do Mile 4 first, then come back.
Quick check: Why is Pandas called a third-party library?
Answer: It is not built into Python; it was made by the community and must be installed.

Sign up or sign in to rate this section
SECTION 3 THE TWO MAIN OBJECTS: SERIES AND DATAFRAME

3 min read

Pandas has two main building blocks. A Series is a single column of data with labels. A DataFrame is a complete table, made of several Series side by side that share the same row labels.

Compare with Python basics: a Series looks like a list that also has labels, and a DataFrame looks like a dictionary of lists drawn as a table. Both are created by calling a tool from Pandas, written pd.Series(...) and pd.DataFrame(...).

The numbers on the left (0, 1, 2) are the index, the label of each row. Pandas adds this automatically unless you choose your own. Each of these ideas gets its own mile later, so just notice them for now.

import pandas as pd

s = pd.Series([10, 20, 30])
print(s)

df = pd.DataFrame({"name": ["Asha", "Ravi", "Meena"], "marks": [80, 90, 75]})
print(df)
print(type(df["marks"]).__name__)

Code explained:

Code What it does
pd.Series([10, 20, 30]) Creates one column from a list
pd.DataFrame({...}) Creates a table from a dictionary of lists
df["marks"] Picks one column out of the table
type(...).name Prints the type name only

Output explained:

Output Meaning
0 10, 1 20, 2 30 Index on the left, values on the right; dtype int64 means whole numbers
The table Column names on top, index on the left, three records
Series One column of a DataFrame is a Series

Watch out: Names are case sensitive: pd.DataFrame works but pd.dataframe or pd.Dataframe does not.
Quick check: Which object represents a full table?
Answer: DataFrame.

 

Sign up or sign in to rate this section
SECTION 4 WHAT PANDAS CAN DO

3 min read

Pandas covers the whole journey of data. First you load data from files such as CSV and Excel. Then you clean it: fix missing values, remove duplicates, correct types. Next you analyse it: filter, group, count and average. Finally you export the result for others.

A missing value is a blank in the table. In Pandas it shows as NaN, short for Not a Number. Dealing with missing values is one of the most common tasks in real work.

Do not worry about understanding each method yet. Read the code below as a story: count the blanks, fill them with 0, then total the amounts for each city.

import pandas as pd
sales = pd.DataFrame({
    "city": ["Chennai", "Delhi", "Chennai", "Mumbai", "Delhi"],
    "amount": [200, 150, None, 300, 100],
})
print(sales.isna().sum())
sales["amount"] = sales["amount"].fillna(0)
print(sales.groupby("city")["amount"].sum())

Code explained:

Code What it does
.isna().sum() Counts missing values per column
.fillna(0) Replaces missing values with 0
.groupby("city")["amount"].sum() Total amount for each city

Output explained:

Output Meaning
amount 1 One missing amount was found
Chennai 200 Missing value became 0 so total is 200
Delhi 250 150 + 100

Watch out: fillna(0) is a choice, not always correct. Median or mean may suit better (Level 2).
Quick check: Which method counts missing values?
Answer: .isna().sum()

Sign up or sign in to rate this section
SECTION 5 WHY PANDAS INSTEAD OF PLAIN PYTHON

3 min read

You saw in Section 1 that even an average needs a loop in plain Python. Grouped totals need even more code: dictionaries, counters, loops inside loops.

Pandas provides these operations as single, well-tested methods. Fewer lines means fewer mistakes, and the code is easier for other people to read. Pandas also runs the calculations in fast compiled code, which matters for large tables.

Below, the same task is done twice: average amount per city, first with plain Python and then with Pandas. Both give the same answer.

import pandas as pd
cities = ["Chennai", "Delhi", "Chennai", "Delhi"]
amounts = [200, 150, 100, 50]

totals = {}
counts = {}
for c, a in zip(cities, amounts):
    totals[c] = totals.get(c, 0) + a
    counts[c] = counts.get(c, 0) + 1
print({c: totals[c] / counts[c] for c in totals})

df = pd.DataFrame({"city": cities, "amount": amounts})
print(df.groupby("city")["amount"].mean().to_dict())

Code explained:

Code What it does
for c, a in zip(...) Manual loop to build totals
dict comprehension Computes averages by hand
groupby(...).mean() Same result in one line

Output explained:

Output Meaning
Both lines Identical averages: Chennai 150.0, Delhi 100.0

Watch out: Fewer lines means fewer bugs, but only if you understand what the method does.
Quick check: Which is shorter for group averages: loop or groupby?
Answer: groupby.

Sign up or sign in to rate this section
SECTION 6 WHERE PANDAS IS USED

2 min read

Anywhere people keep tables, Pandas is useful. Banks use it for transaction checks, shops for sales reports, hospitals for patient statistics, and technology companies for logs and machine-learning data.

That is why Pandas appears in job descriptions for data analyst, data scientist, machine learning and data engineering roles. The next mile looks at these careers in detail.

import pandas as pd
uses = pd.DataFrame({
    "industry": ["Finance", "Retail", "Healthcare", "Tech"],
    "typical_task": ["Fraud checks", "Sales report", "Patient stats", "Log analysis"],
})
print(uses.to_string(index=False))

Code explained:

Code What it does
.to_string(index=False) Prints the table without the row numbers

Output explained:

Output Meaning
Each row One industry and a typical Pandas task

Watch out: Pandas keeps data in memory (RAM). Very large data needs chunks or other tools (Level 4).
Quick check: Name two industries that use Pandas.
Answer: For example finance and retail.

Sign up or sign in to rate this section

📝 Assignment

Available Assignments
Assignment Pass Mark Duration Actions
Assignment: What Is Pandas and Why We Use It
70% 01:00:00

📊 Exam

Available Exams
Exam Pass Mark Duration Questions Actions
Exam: What Is Pandas and Why We Use It
70% 00:30:00 All

💬 Comments & Discussion

Sign up free or sign in to post a comment
Sign up free or sign in
No comments yet

Be the first to share your thoughts!