pandas basics · lesson 4 of 9
Parts in this lesson
Adf[["store", "kilos"]]several columns: a list inside the brackets
Bdf.loc[rows, columns]by label
Cdf.iloc[rows, columns]by position
Ddf[df["kilos"] > 33]2.5: a mask, now choosing rows

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 .loc and by position with .iloc
  • Filter rows with masks, query, isin and between
  • 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.

Which cells each selection picks. sales["kilos"]: one column, by name. You get a Series.2024-01-02Batangas4245.02024-01-02Lipa3546.02024-01-02Tanauan2945.02024-01-03Batangas3845.02024-01-03Lipa3346.02024-01-03Tanauan3044.02024-01-04Batangas4045.02024-01-04Lipa3146.02024-01-04Tanauan3444.0sales["kilos"]datestorekilosprice012345678
1 of 6 sales["kilos"]: one column, by name. You get a Series.
select.ipynb
  • 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 are store and kilos.

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.

filter.ipynb
  • A mask, from lesson 2.5: sales["kilos"] > 33 is True or False for each row, and sales[mask] keeps the True rows.
  • Two conditions: & for and, | for or, with brackets round each condition. and and or give the same ValueError as in lesson 2.5.
  • query writes the same filter as text: "store == 'Lipa' and kilos > 32". Shorter, and it can use plain and. 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.

first-three.ipynb

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

Which gives a DataFrame with just the store and price columns?
The index is 0 to 99. How many rows does df.loc[10:20] give?
You want the rows where kilos is from 30 to 40, including 30 and 40. Which works?

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.

All sources for Simple Data Analysis