What's the alternative to pandas chain indexing?

你说的曾经没有我的故事 提交于 2019-12-17 21:29:50

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/41253170/whats-the-alternative-to-pandas-chain-indexing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!