按整数索引 pandas 数据帧 [英] Indexing a pandas dataframe by integer
问题描述
我似乎找不到一种优雅的方式来索引 pandas.DataFrame
整数索引.在以下示例中,我想从 'A'
列的第一个元素中获取值 'a'.
I can't seem to find an elegant way to index a pandas.DataFrame
by an integer index.
In the following example I want to get the value 'a' from the first element of the 'A'
column.
import pandas
df = pandas.DataFrame(
{'A':['a','b', 'c'], 'B':['f', 'g', 'h']},
index=[10,20,30]
)
我希望 df['A'].ix[0]
和 df['A'][10]
都返回 'a'代码>.
df['A'][10]
确实返回 'a'
,但是 df['A'].ix[0]
抛出KeyError: 0
.我能想到的根据索引 0 获取值 'a'
的唯一方法是使用以下方法.
I would expect df['A'].ix[0]
and df['A'][10]
both to return 'a'
.
The df['A'][10]
does return 'a'
, but df['A'].ix[0]
throws a KeyError: 0
. The only way I could think of to get the value 'a'
based on the index 0 is to use the following approach.
df['A'][df['A'].index[0]]
是否有更短的方法可以使用 0 索引从数据帧中获取 'a'
?
Is there a shorter way to get 'a'
out of the dataframe, using the 0 index?
更新
从 pandas 0.11 开始,还有另一种方法可以索引按整数.
As of pandas 0.11 there is a another way to index by integer.
df.iloc[0] # integer based, gives the first row
df.loc[10] # label based, gives the row with label 10
这个取代irow代码>方法.
推荐答案
df['A'].ix[0]
出现错误,因为您的索引不是从 0 开始的,它从 10 开始.您可以通过以下任一方式获得您想要的值
You get an error with df['A'].ix[0]
because your indexing doesn't start at 0, it starts at 10. You can get the value you want with either of the following
df['A'].ix[10]
df['A'].irow(0)
第一个使用正确的索引.第二个命令,我怀疑是你想要的,通过行号而不是索引值找到值,技术上只比 if df['A'].ix[0]<长两个字符/code> 工作.
The first uses by the correct index. The second command, which I suspect is what you want, finds the value by the row number, rather than by index value, and is technically only two characters longer than if df['A'].ix[0]
worked.
或者,您可以重置索引,以便它们以您期望的方式响应 df['A'].ix[0]
:
Alternatively, you can reset the indices so that they will respond the way you expect for df['A'].ix[0]
:
df2=df.reset_index()
这将通过将旧索引(10、20 等)移动到 df2 数据框中名为index"的列中来保留它们.然后 df2['A'].ix[0]
将返回 'a'.如果要删除旧的基于 10 的索引,可以在 reset_index 函数的括号中插入标志 drop=True
.
This will preserve your old indices (10, 20, etc.) by moving them into a column called "index" in the df2 data frame. Then df2['A'].ix[0]
will return 'a'. If you want to remove the old 10-based indices, you can insert the flag drop=True
into the parenthesis of the reset_index function.
这篇关于按整数索引 pandas 数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!