{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
⚠ Stop: It's vital that you understand how this code works. By which I mean why it does something at all, not exactly how to use loc and iloc (though that is also useful).
\n", "df.set_index(...) changes the index from the default row number to another field in the data frame. This operation returns a new data frame with listing_url as its index. Because set index returned a data frame, we can simply add another method call (iloc or loc) on to the end of that line and it returns a new data frame in turn! \n", "
The fact that each operation returns a new data frame (or data series) is why you can even do this:
\n", " \n", " df.set_index('listing_url').iloc[0:3].latitude.mean()\n", "