AI Hints
Home›Data Science Tutorial›Pandas in Depth
Data Science · Lesson 4 of 101

Pandas Essentials

Select, filter, and sort.

⏱ 2 min read▶ Video at 7:54🎓 Free
Watch this lesson in the Data Science courseWatch on YouTube ↗Full Data Science course

Pandas is to a data scientist what a kitchen knife is to a chef. You'll use it every day, so it needs to feel automatic. Over the next few lessons, we'll build that skill on Harbor Coffee's real sales data.

Pandas Essentials
From the video: Pandas Essentials
  • The chef's knife of data
  • You'll use it every day
  • Build the habit

First moves with any table

With any new table, ask three things. How big is it? What's in each column? And what does it look like? Shape, d types, and head answer those. Run it. Over seventy thousand rows. Dates are real dates, because we asked for them when loading. Each row is one product, at one cafe, on one day. Knowing what one row means is the most important fact about any table.

first_moves.pypython
import pandas as pd

sales = pd.read_csv("sales.csv", parse_dates=["date"])

print(sales.shape)
print(sales.dtypes.to_string())
print(sales.head(3).to_string())
Output
(70176, 10)
date        datetime64[us]
store_id             int64
product                str
units                int64
category               str
discount           float64
revenue            float64
profit             float64
city                   str
region                 str
        date  store_id   product  units category  discount  revenue  profit    city    region
0 2024-01-01         1  Espresso     26   Coffee       0.0     78.0    57.2    Oslo    Europe
1 2024-01-01         2  Espresso     40   Coffee       0.0    120.0    88.0  Bergen    Europe
2 2024-01-01         3  Espresso     26   Coffee       0.0     78.0    57.2    Lima  Americas

Filtering rows

To filter, put a condition inside square brackets. Pandas keeps the rows where it's true. You can stack filters. For two conditions, use an ampersand, with brackets around each one. Query does the same, in a style that's easy to read. Run it. Answers to specific questions, in a line each. How many lattes did Rome sell? Done.

filters.pypython
import pandas as pd

sales = pd.read_csv("sales.csv", parse_dates=["date"])

rome = sales[sales["city"] == "Rome"]
latte = rome[rome["product"] == "Latte"]
big = sales[(sales["units"] > 60) & (sales["discount"] > 0)]

print("Rome rows:", len(rome))
print("Rome lattes:", latte["units"].sum())
print("Big discounted days:", len(big))
print(sales.query("city == 'Oslo' and units > 50").shape)
Output
Rome rows: 5848
Rome lattes: 33363
Big discounted days: 137
(30, 10)

Summary

Three questions for each new table, and square brackets for filtering. Now let's get to the real power of pandas. Next, grouping.

Key takeaways
  • shape, dtypes, head
  • Filter with conditions
  • Know what one row means
Next up: Grouping and Aggregating — split, apply, combine