From the course: pandas Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Using loc[]

Using loc[] - pandas Tutorial

From the course: pandas Essential Training

Start my 1-month free trial

Using loc[]

- [Announcer] "loc[]" is a label-based indexer, that means you are selecting by the labels. And notice that "loc[]" uses square brackets and not regular brackets. "loc[]" will raise the KeyError when items are not found. Let's head over to the jupyter Notebook to look at a couple of examples. If I wanted to search the Olympic table by the athlete's names, I could try for example, "oo.", which is the name of my data frame, "loc" and if I look for Usain Bolt, I end up with a KeyError saying "The label [BOLT, USAIN] Is not in the {index}". Why might this be the case? If I look at my data frame again, I can see that the index is not the athlete's name, but is the integer based index. So what I can do is I can set the index to the athlete's name, set inplace to true and try again. Bolt, Usain. And you can see now that I get a list of rows of all of the medals that Usain Bolt has won in the Olympics. Now, you might think that the only way to access Usain Bolt's entry is if we set the index…

Contents