runtime
207 / 250
lesson 207 pandas Foundations · advanced

Selecting rows and columns

loc selects by label, iloc by position. With a meaningful index such as the order ID, loc answers lookups directly; after sort_values, iloc takes the top rows.

You'll be able to
  • Select several columns with a list, and one cell or row with loc[label, column]
  • Use set_index to look rows up by a key, and check membership with in df.index
  • Order rows with sort_values and take positions with iloc, knowing that a loc slice includes its end
The idea

Two kinds of question pick rows. *Show me order O100376* is a label lookup: loc. *Show me the three biggest orders* is a position question after ordering: sort_values then iloc. Inside loc[...] and iloc[...] the row comes first and the column second, as in NumPy. set_index("order_id") makes a column the row labels, so loc can find a row by its ID.

Run each example to see what it prints. Running examples doesn't count toward progress.

example-1.py · Look orders up by ID
import pandas as pd

orders = pd.DataFrame([
    {"order_id": "O100282", "status": "delivered", "ship_state": "TX", "total": 35.84},
    {"order_id": "O100376", "status": "returned", "ship_state": "CA", "total": 91.47},
    {"order_id": "O100918", "status": "returned", "ship_state": "NY", "total": 55.72},
    {"order_id": "O101219", "status": "delivered", "ship_state": "TX", "total": 246.6},
]).set_index("order_id")
print(orders.loc["O100376", "total"])
print(orders.loc["O100918", ["status", "ship_state"]].tolist())
print(orders.loc["O100376":"O101219", ["status", "total"]])
print("O999999" in orders.index)
example-2.py · A position given to loc
import pandas as pd

orders = pd.DataFrame([{"order_id": "O100282", "total": 35.84}]).set_index("order_id")
print(orders.loc[0])
example-3.py · The three largest orders
import pandas as pd

orders = pd.DataFrame([
    {"order_id": "O100282", "channel": "web", "total": 35.84},
    {"order_id": "O100376", "channel": "web", "total": 91.47},
    {"order_id": "O101219", "channel": "web", "total": 246.6},
    {"order_id": "O101723", "channel": "phone", "total": 54.55},
    {"order_id": "O103199", "channel": "app", "total": 64.17},
])
ranked = orders.sort_values("total", ascending=False)
print(ranked.iloc[:3]["order_id"].tolist())
print(ranked.iloc[0]["channel"], ranked.index[0])
print(orders[["order_id", "total"]].shape)

When to use which: loc when you know the label (an ID, a date), iloc when you know the position (first, last, top n after sorting), df[[...]] to keep only some columns. Mistakes to avoid: df["a", "b"] (one key that is a tuple, a KeyError) instead of df[["a", "b"]]; mixing up labels and positions after sort_values or set_index; and forgetting that a loc slice includes its end.

Review
  • loc[row_label, column] selects by label (slices include the end); iloc selects by position.
  • set_index makes a key column the labels; in df.index checks one exists.
  • sort_values then iloc[:n] gives the top n; df[[...]] keeps chosen columns.
  • Next: keep only the rows that pass a test.
Practise this

Loading your progress…

To master this lesson, solve both challenges without hints or the answer, and pass the concept check.

Selecting rows and columns — pandas Foundations · Runtime