Accessing a Pandas index like a regular column

后端 未结 3 2092
后悔当初
后悔当初 2021-02-19 02:29

I have a Pandas DataFrame with a named index. I want to pass it off to a piece off code that takes a DataFrame, a column name, and some other stuff, and does a bunch of work inv

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-19 03:03

    You could also use df.index.get_level_values if you need to access a (index) column by name. It also works with hierarchical indices (MultiIndex).

    >>> df.index.get_level_values('name')
    Index(['a', 'b', 'c', 'd', 'e'], dtype='object', name='name')
    

提交回复
热议问题