Module 5 · Pandas: Your Data Workbench · Lesson 15 of 51

DataFrames: load, look, select and filter

Series and DataFrames, read_csv, head, info, describe and value_counts, selecting columns, loc and iloc, filtering rows, sorting and new columns.

In this lesson · about 45 minutes

  • Load a CSV and get a first overview with head, info, describe and value_counts
  • Select columns and rows with [], loc and iloc
  • Filter rows with conditions, sort, and add calculated columns

First look: always these five

orders = pd.read_csv("kc_orders.csv", parse_dates=["date"])
orders.head()            # first 5 rows
orders.shape             # (rows, columns)
orders.info()            # column names, types, non-missing counts
orders.describe()        # count, mean, std, min, quartiles, max of number columns
orders["branch"].value_counts()   # how often each value appears

parse_dates turns the date text into real dates, so you can group by month later.

Selecting

  • One column (a Series): orders["total"]. Several (a DataFrame): orders[["branch", "total"]]. Note the double brackets.
  • .loc selects by label: orders.loc[0:4, "branch":"item"] (label slices include the end).
  • .iloc selects by position: orders.iloc[:5, :3] (like NumPy; the end is excluded).

Filtering

It's a NumPy boolean mask on a table: orders[orders["total"] > 20]. Combine with & and | and brackets: orders[(orders["branch"] == "Campus") & (orders["hour"] < 9)]. Handy helpers: .isin(["Card", "App"]), .between(5, 10), .str.contains("tea", case=False), and orders.query("total > 20 and branch == 'Campus'").

Sorting and new columns

orders.sort_values("total", ascending=False); orders.nlargest(5, "total"). New column: orders["revenue_ex_tax"] = orders["total"] / 1.08. Date parts: orders["month"] = orders["date"].dt.month, .dt.day_name().

First look at six months of orders

Select, filter, sort, add columns

Exercise

Weekend food orders

orders is loaded with a weekday column. Set weekend_food to the orders in category "Food" placed on "Saturday" or "Sunday", keeping only the columns order_id, branch, item, total, sorted by total descending then order_id. Set n to how many there are.

Code that runs before yours
import pandas as pd
orders = pd.read_csv("kc_orders.csv", parse_dates=["date"])
orders["weekday"] = orders["date"].dt.day_name()

Exercise

Morning orders and their share

Add a column daypart to orders that is "Morning" when hour is below 11 and "Later" otherwise (use np.where). Then set morning_share to the share of all orders that are Morning, rounded to 3 decimals.

Code that runs before yours
import pandas as pd
orders = pd.read_csv("kc_orders.csv", parse_dates=["date"])

Check your understanding0 of 4 correct

Question 1

1.orders[['branch', 'total']] returns…

Question 2

2.loc vs iloc:

Question 3

3.Which shows how often each payment method appears?

Question 4

4.What does parse_dates=['date'] do?

Quiz score: 0 of 4

Exercises solved: 0 of 2

Tip: answer every quiz question correctly first.

Written by Noviq Academy in its own words. Kettle & Crumb and all its data are made up for teaching; any resemblance to a real business is a coincidence. Python runs in your browser with Pyodide, so results can differ slightly from your own computer. Power BI, Tableau and other product names belong to their owners. Read-aloud uses your device's built-in voices.