Choosing columns and rows
Last updated
Every question about a table starts by choosing part of it: one column, the rows for one store, the days over 33 kg. pandas has several ways to choose, and the two that matter most, .loc and .iloc, look almost the same but count differently. In this lesson you'll learn them all, and when to use which.
By the end of this lesson you can
- Choose one or several columns with square brackets
- Choose rows and columns by label with
.locand by position with.iloc - Filter rows with masks,
query,isinandbetween - Say why
.loc[0:3]gives four rows and.iloc[0:3]three
Columns, and .loc and .iloc
Step through which cells each expression picks, then run them.
sales["kilos"]: one column, by name. You get a Series.- One column:
sales["kilos"], a Series. - Several columns:
sales[["store", "kilos"]], a DataFrame. Note the two pairs of brackets: the outer ones choose, the inner ones make a list of names. .loc[rows, columns]chooses by label: the index labels for rows, the names for columns.sales.loc[2, "kilos"]is 29..iloc[rows, columns]chooses by position, exactly like NumPy in lesson 2.5.sales.iloc[0:2, 1:3]is rows 0 and 1, and columns 1 and 2, which arestoreandkilos.
The "i" in iloc is for integer position. If you know a column's name or a row's label, use .loc: the code says what it means, and it still works after someone adds a column at the front.
Filtering rows
Most of the time you choose rows by what's in them. Run the cells.
- A mask, from lesson 2.5:
sales["kilos"] > 33isTrueorFalsefor each row, andsales[mask]keeps theTruerows. - Two conditions:
&for and,|for or, with brackets round each condition.andandorgive the sameValueErroras in lesson 2.5. querywrites the same filter as text:"store == 'Lipa' and kilos > 32". Shorter, and it can use plainand. Notice the text needs its own quotes round'Lipa'.isin: rows whose value is in a list. Clearer than(s == "Lipa") | (s == "Tanauan").between(30, 35): from 30 to 35, including both ends.
Look at the index of each answer. The rows keep their labels, 1 and 4 for Lipa, not 0 and 1. That's how you can always tell which rows of the original you've got.
Wrong way: four rows or three?
You want the first three rows. Run both.
sales.loc[0:3] gives four rows, labelled 0 to 3. sales.iloc[0:3] gives three, positions 0 to 2.
The reason: a label slice includes its end, and a position slice doesn't. Positions follow Python's rule (stop before the end), because you know how many there are. Labels can be anything, even dates or store names, and sales.loc["Batangas":"Lipa"] would be odd if it left out Lipa. So .loc includes both ends.
It's easy to miss because the default index makes labels and positions the same numbers. They stop being the same after you sort or filter. So: .iloc when you mean "the first three"; .loc when you mean "the rows labelled…". And for the first few rows, there's head(3).
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- pandas: Indexing and selecting data: .loc, .iloc, boolean indexing, isin and query
- pandas: pandas.DataFrame.loc: label slices include both ends
- pandas: pandas.DataFrame.query: a filter written as text
- pandas: pandas.Series.between: both ends included by default