像常规列一样访问 pandas 索引 [英] Accessing a Pandas index like a regular column

查看:48
本文介绍了像常规列一样访问 pandas 索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有命名索引的Pandas DataFrame.我想将其传递给一段代码,该代码采用一个DataFrame,一个列名和其他一些东西,并完成涉及该列的大量工作.仅在这种情况下,我要突出显示的列是索引,但是为该代码段提供索引的标签不起作用,因为您无法像常规列那样提取索引.例如,我可以像这样构造一个DataFrame:

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 involving that column. Only in this case the column I want to highlight is the index, but giving the index's label to this piece of code doesn't work because you can't extract an index like you can a regular column. For example, I can construct a DataFrame like this:

import pandas as pd, numpy as np

df=pd.DataFrame({'name':map(chr, range(97, 102)), 'id':range(10000,10005), 'value':np.random.randn(5)})
df.set_index('name', inplace=True)

结果如下:

         id     value
name                 
a     10000  0.659710
b     10001  1.001821
c     10002 -0.197576
d     10003 -0.569181
e     10004 -0.882097

现在如何允许我访问 name 列?

Now how am I allowed to go about accessing the name column?

print(df.index)  # No problem
print(df['name'])  # KeyError: u'name'

我知道有一些解决方法,例如复制列或将索引更改为其他内容.但是,是否有某种更清洁的方法,例如某种形式的列访问,可以像对待其他所有索引一样对待索引?

I know there are workaround like duplicating the column or changing the index to something else. But is there something cleaner, like some form of column access that treats the index the same way as everything else?

推荐答案

索引在Pandas中具有特殊含义.它用于优化特定操作,并可用于各种方法,例如合并/联接数据.因此,请选择:

Index has a special meaning in Pandas. It's used to optimise specific operations and can be used in various methods such as merging / joining data. Therefore, make a choice:

  • 如果它只是另一列",请使用 reset_index 并将其视为另一列.
  • 如果它确实用于索引编制,请将其保留为索引并使用 df.index .
  • If it's "just another column", use reset_index and treat it as another column.
  • If it's genuinely used for indexing, keep it as an index and use df.index.

我们无法为您做出选择.它应取决于基础数据的结构以及打算如何分析数据.

We can't make this choice for you. It should be dependent on the structure of your underlying data and on how you intend to analyse your data.

有关使用数据框索引的更多信息,请参见:

For more information on use of a dataframe index, see:

这篇关于像常规列一样访问 pandas 索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆