01 / 10

Session 2

Exploring data with Pandas

Loading • Inspecting • Filtering
• Asking questions

Bloomberg × Asian College of Journalism

Quick check-in

Before we begin

  • Did everyone manage to install Python?
  • VS Code?
  • Jupyter Notebook?
Common installation issuepip missing
No module named pip
Python
↓ pip ↓ Jupyter

Sometimes Python installs successfully but pip isn't available.

python --version ↓ python -m pip --version ↓ python -m ensurepip --upgrade ↓ python -m pip install notebook
Load data
↓
Inspect it
↓
Ask a question
↓
Check the answer

Pandas is less about memorising commands and more about recognising patterns.

A dataframe is just a table

Table
stored inside
df
df = pd.read_excel(...)
ObjectMemory
df │ └── our dataset

Inspect before analysing

head()

Look at the first rows.

tail()

See the end of the table.

shape

Count rows and columns.

info()

Check types and missing data.

Filtering is a checklist

Stage 1table
State TFR Kerala 1.7 Bihar 2.9 Tamil Nadu 1.6 UP 2.7 Goa 1.5
Stage 2question
TFR < 2 ?
Stage 3keep True
True False True False True
Resultrows kept
Kerala Tamil Nadu Goa

Filtering is simply creating a True/False checklist and keeping the rows marked True.

Reading Pandas like English

CodeMeaning
df.head()
Take the dataframe and show me the first five rows.
df.sort_values(...)
Take the dataframe, sort it, show the result.
df[df["x"] > 5]
Take the dataframe. Keep rows where x is greater than 5.

Debugging isn't failure

KeyError

Wrong column name.

NameError

Python doesn't recognise the name. Usually missing quotes or variable.

SyntaxError

Check brackets. Quotes. Parentheses.

Empty dataframe

Your code worked. No rows matched the condition.

Remember

Load
↓
Inspect
↓
Question
↓
Verify