I'm taking an online class to learn python and the instructor taught us that chain indexing was not a good idea. However, he failed to tell is the appropriate alternative to use.
Suppose I have a Pandas data frame with rows indexed as ['1', '2', '3']
and columns with names ['a', 'b', 'c']
.
What's the appropriate alternative to using the command df['1']['a']
to extract the value found in the first row and first column?
Use multi-axis indexing, e.g.
When you use
df['1']['a']
, you are first accessing the series objects = df['1']
, and then accessing the series elements['a']
, resulting in two__getitem__
calls, both of which are heavily overloaded (handle a lot of scenarios, like slicing, boolean mask indexing, and so on).It's much more efficient to use the
df.loc
indexer.