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.
- Select several columns with a list, and one cell or row with
loc[label, column] - Use
set_indexto look rows up by a key, and check membership within df.index - Order rows with
sort_valuesand take positions withiloc, knowing that alocslice includes its end
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.
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)
import pandas as pd orders = pd.DataFrame([{"order_id": "O100282", "total": 35.84}]).set_index("order_id") print(orders.loc[0])
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.
loc[row_label, column]selects by label (slices include the end);ilocselects by position.set_indexmakes a key column the labels;in df.indexchecks one exists.sort_valuestheniloc[:n]gives the top n;df[[...]]keeps chosen columns.- Next: keep only the rows that pass a test.