Pandas中.iloc API的索引

huangapple go评论56阅读模式
英文:

Index of .iloc API in Pandas

问题

如上面info()结果所示,我的数据集DF中有11列,索引从0到10。现在,我想提取前10列(即索引从0到9的列)。然而,当我尝试使用下面的代码:

DF.iloc[:, 0:9]

它只返回前9列(从CompPrice到Urban)。

在这种情况下,我需要将我的代码更改为:

DF.iloc[:, 0:10]

才能获得我真正想要的结果(即从CompPrice到US)。

我对iloc()的索引感到困惑。为什么它需要'10'而不是'9',但起始索引却是'0'。起始和结束索引不一致。

英文:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 400 entries, 0 to 399
Data columns (total 11 columns):
 #   Column       Non-Null Count  Dtype 
---  ------       --------------  ----- 
 0   CompPrice    400 non-null    int64 
 1   Income       400 non-null    int64 
 2   Advertising  400 non-null    int64 
 3   Population   400 non-null    int64 
 4   Price        400 non-null    int64 
 5   ShelveLoc    400 non-null    object
 6   Age          400 non-null    int64 
 7   Education    400 non-null    int64 
 8   Urban        400 non-null    object
 9   US           400 non-null    object
 10  HighSales    400 non-null    object
dtypes: int64(7), object(4)
memory usage: 34.5+ KB

As shown in the info() result above, there are 11 columns indexed from 0 to 10 in my dataset, DF. Now, I would like to extract only the first 10 columns (that are the columns with the indices 0 to 9). However, when I try to use the code below:

DF.iloc[:, 0:9]

It returns only the first 9 columns (that is, from CompPrice to Urban).

In this case, I need to change my code to:

DF.iloc[:, 0:10]

to get what I actually want (that is, from CompPrice to US).

I'm really confused by iloc() indices. Why it requires '10' instead '9' but starts with the index '0'. The starting and ending indices are not consistent.

答案1

得分: 2

短答案是因为这是 Python 索引的工作方式。Pandas 使用 iloc 进行行选择与 Python 索引保持一致。考虑以下列表:

lst = ['a', 'b', 'c', 'd', 'e', 'f']
  • lst[0:1] 返回索引 0 到 1-1:['a']

  • lst[0:2] 返回索引 0 到 2-1:['a', 'b']

  • lst[0:3] 返回索引 0 到 3-1:['a', 'b', 'c']

  • [0:n] 总是返回索引 0 到 n-1。

Pandas 行为方式相同。

英文:

The short answer is because this is how Python indexing works. Pandas row selection with iloc is consistent with Python indexing. Consider the list:

lst = ['a', 'b', 'c', 'd', 'e', 'f']
  • lst[0:1] returns index 0 to 1-1: ['a']

  • lst[0:2] returns index 0 to 2-1: ['a', 'b']

  • lst[0:3] returns index 0 to 3-1: ['a', 'b', 'c']

  • [0:n] always returns index 0 to n-1.

Pandas behaves the same way.

答案2

得分: 1

你所观察到的是pandas的标准功能。如果您查看文档,可以找到定义。这是有意的和合乎逻辑的,因为Python列表的功能方式相同。根据文档:

>.iloc 主要基于整数位置(从轴的0到长度-1),但也可以与布尔数组一起使用。如果请求的索引器超出边界,.iloc 将引发 IndexError,但切片索引器允许超出边界的索引(这符合Python/NumPy切片语义)。

英文:

What you are observing is the standard functionality of pandas. If you look in the documentation, you can find the definition. This is intended and logical, as Python lists function the same way. As per the docs:

>.iloc is primarily integer position based (from 0 to length-1 of the axis), but may also be used with a boolean array. .iloc will raise IndexError if a requested indexer is out-of-bounds, except slice indexers which allow out-of-bounds indexing. (this conforms with Python/NumPy slice semantics).

答案3

得分: 1

按照惯例,索引范围为 [start, stop),意味着起始索引被包括在内,但停止索引被排除在外。

range(1, 10) # 返回 1, 2, 3, 4, 5, 6, 7, 8, 9,但不包括 10

英文:

By convention, index ranges are [start, stop) meaning that the start index is included but the stop index is excluded.

range(1, 10) # returns 1, 2, 3, 4, 5, 6, 7, 8, 9 but not 10

huangapple
  • 本文由 发表于 2023年6月2日 03:25:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定