pandas · Chapter 5 of 22

Rows by label and by position: loc and iloc

By the end of this chapter you can pick any rows and columns of a DataFrame: by their labels with .loc, by their positions with .iloc, and one cell with .at and .iat; say which rows a slice keeps (a label slice keeps its end, a position slice does not); and stay out of the trap of a table whose labels are numbers that are not its positions. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Tables from zero
  • 4 tasks
  • pandas
  • In the all labs plan

The first 2 chapters of pandas are free. This one comes with the all labs plan.

Why you need this

Harpreet answers the same kind of question many times a day. "What is KG-SAR-019's price and fabric?" is one row and two columns, by sku. "Send me the three cheapest stoles" is the first three rows of a table sorted by price, by position. "Every saree from KG-SAR-010 to KG-SAR-020, just the prices" is a block of rows and columns.

So far you can only pick columns. Square brackets on a DataFrame look in the columns, so a row label in them is a KeyError, and yet a slice in them picks rows, by position:

import pandas as pd

weavers = pd.read_csv("/workspace/data/weavers.csv")
print(weavers[0:2])
print(weavers[0])
  weaver_id                name       village  looms      joined
0       W01  Abdul Kalam Ansari  Sarai Mohana      2  2019-06-17
1       W02         Rehana Bano  Sarai Mohana      2  2022-12-02
KeyError: 0

One pair of brackets doing two different things is confusing, and worse once the labels are not 0, 1, 2. pandas has two selectors that each do one thing: .loc works only by label, .iloc only by position.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    product.py: products by sku

    Write two functions in product.py. Each is given by_sku, the catalogue labelled by sku, as pd.read_csv(path, index_col="sku") reads products.csv: the columns name, category, fabric, weaver_id, cost_rs, mrp_rs and launched.

    one_product(by_sku, sku) returns that product's row as a Series: labelled by the column names, in the table's order, with the sku as the Series' name.

    some_products(by_sku, skus, columns) is given a list of skus and a list of column names. It returns a DataFrame of those rows, in the order of skus, labelled by sku, with only those columns, in the order of columns.

    The Check tries several skus and lists (one list of a single sku), and a small catalogue of 3 products labelled by sku with other columns.

  2. Task 2

    ends.py: the ends of a sorted table, by position

    Write two functions in ends.py. Each is given a table whose rows have been sorted, so its row labels are no longer 0, 1, 2 from the top.

    first_and_last(table) returns a DataFrame of two rows: the table's first row and then its last, by position, with every column and their own labels. A table of one row gives that row twice.

    nth_value(table, n, column) returns the one value in the column named column, in the row at position n, where 0 is the first row and -1 the last.

    The Check sorts its tables first: the catalogue by price (labels 91, 82, 88 at the top), the weavers by looms (most first), and a table of one row.

  3. Task 3

    block.py: a block of the catalogue, by label

    Write price_block(by_sku, first, last) in block.py. by_sku is the catalogue labelled by sku, as in task 1; first and last are two skus, first coming before last in the table (or the same sku).

    Return a DataFrame of the rows from the sku first to the sku last, both included, in the table's order, labelled by sku, and the columns from category to mrp_rs, both included, in the table's order. For products.csv the columns are category, fabric, weaver_id, cost_rs and mrp_rs.

    The Check tries several ranges, one of a single sku, and a small catalogue whose columns between category and mrp_rs are not the same as products.csv's, so do not type the column names in.

  4. Task 4 · interview question

    Interview: first_three.py, three rows that came out four

    Harpreet's report shows the first three rows of a table, by position. Her first_three(table) gives four rows on a table labelled 0, 1, 2 and so on, and odd rows on a sorted one. Fix it so it returns a DataFrame of the table's first three rows by position, with every column and their own labels. A table of fewer than three rows gives all of its rows. first_three(weavers) is a DataFrame of 3 rows labelled 0, 1 and 2.

    The Check tries the weavers, the catalogue sorted by price (labels 91, 82, 88 at the top) and a table of two rows.

    Devika will ask: why did .loc[0:3] give four rows and .iloc[0:3] three?

What you will learn

  • [] is for columns; why it is confusing on rows.
  • .loc[rows, columns]: by label; one label, a list, or a slice that includes its end.
  • .iloc[rows, columns]: by position, from 0; a slice that excludes its end, and negative positions from the end.
  • A row is a Series: the column names become its labels.
  • .at and .iat: one cell, by label or by position.
  • The trap: number labels that are not positions, KeyError against IndexError, and which selector to reach for.

More in Tables from zero

Build "Rows by label and by position: loc and iloc" yourself

Kargha, a Varanasi handloom brand, sells through a website, a marketplace and a store. You make one true picture of its sales. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of pandas are free. This one comes with the all labs plan.