Python pandas:为什么我的训练数据的 df.iloc[:, :-1].values 只选择到倒数第二列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37512079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python pandas: Why does df.iloc[:, :-1].values for my training data select till only the second last column?
提问by kwotsin
Very simply put,
很简单的说,
For the same training data frame df
, when I use
X = df.iloc[:, :-1].values
, it will select till the second last column of the data frame instead of the last column (which is what I want BUT it's a strange behavior I've never seen before), and I know this as the second last column's value and the last column's value for that row is different.
对于相同的训练数据框df
,当我使用时
X = df.iloc[:, :-1].values
,它将选择直到数据框的倒数第二列而不是最后一列(这是我想要的,但这是我以前从未见过的奇怪行为),我知道这作为倒数第二列的值和该行的最后一列的值不同。
However, using
y = df.iloc[:, -1].values
gives me the row vector of the last column's values which is exactly what I want.
但是, using
y = df.iloc[:, -1].values
给了我最后一列值的行向量,这正是我想要的。
Why is the negative 1 for X giving me the second last column's value instead?
为什么 X 的负 1 给我倒数第二列的值?
回答by jezrael
I think you have only two columns in df
, because if there is more columns, iloc
select all columns without last:
我认为您只有两列df
,因为如果有更多列,请iloc
选择没有最后一个的所有列:
df = pd.DataFrame({'A':[1,2,3],
'B':[4,5,6],
'C':[7,8,9],
'D':[1,3,5],
'E':[5,3,6],
'F':[7,4,3]})
print (df)
A B C D E F
0 1 4 7 1 5 7
1 2 5 8 3 3 4
2 3 6 9 5 6 3
print(df.iloc[:, :-1])
A B C D E
0 1 4 7 1 5
1 2 5 8 3 3
2 3 6 9 5 6
X = df.iloc[:, :-1].values
print (X)
[[1 4 7 1 5]
[2 5 8 3 3]
[3 6 9 5 6]]
print (X.shape)
(3, 5)
回答by piRSquared
Just for clarity
只是为了清楚
With respect to python syntax, this question has been answered here.
Python list slicing syntax states that for a:b
it will get a
and everything upto but not including b
. a:
will get a
and everything after it. :b
will get everything before b
but not b
. The list index of -1
refers to the last element. :-1
adheres to the same standards as above in that this gets everything before the last element but not the last element. If you want the last element included use :
.
Python 列表切片语法指出,因为a:b
它将获取a
所有内容,但不包括b
. a:
会得到a
它之后的一切。 :b
会得到之前的一切,b
但不会b
。的列表索引-1
指的是最后一个元素。 :-1
遵循与上述相同的标准,因为这会获取最后一个元素之前的所有内容,但不会获取最后一个元素。如果您希望包含最后一个元素,请使用:
.
回答by Manoj Kumar
Bcz Upper bound is exclusive. Its similar to slicing a list:
Bcz 上限是独占的。它类似于切片列表:
a=[1,2,3,4]
a[:3]
will result in [1, 2, 3]. It did not take the last element.
将导致 [1, 2, 3]。它没有采用最后一个元素。