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

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

问题描述

我有一个带有命名索引的 Pandas 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?

推荐答案

Index 在 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天全站免登陆