What's the alternative to pandas chain indexing?

前端 未结 1 990
悲哀的现实
悲哀的现实 2020-11-29 13:01

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

相关标签:
1条回答
  • 2020-11-29 13:52

    Use multi-axis indexing, e.g.

    df.loc['a', '1']
    

    When you use df['1']['a'], you are first accessing the series object s = df['1'], and then accessing the series element s['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.

    0 讨论(0)
提交回复
热议问题