[译]如何根据Pandas中的列名获取列所在的index位置?

原文来源:https://stackoverflow.com/questions/13021654/get-column-index-from-column-name-in-python-pandas

可以使用 .get_loc实现。

In [45]: df = DataFrame({"pear": [1,2,3], "apple": [2,3,4], "orange": [3,4,5]})

In [46]: df.columns
Out[46]: Index([apple, orange, pear], dtype=object)

In [47]: df.columns.get_loc("pear")
Out[47]: 2
posted @ 2019-08-14 16:05  bingo彬哥  阅读(9752)  评论(0编辑  收藏  举报
本站总访问量