Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Pandas中使用iloc和负整数进行切片_Python_Pandas_Machine Learning - Fatal编程技术网

Python 在Pandas中使用iloc和负整数进行切片

Python 在Pandas中使用iloc和负整数进行切片,python,pandas,machine-learning,Python,Pandas,Machine Learning,我一直在学习Python线性回归教程: 使用以下数据集: 我的问题在于以下代码: x = dataset.iloc[:, :-1].values 否定(-1)在这里做什么?如果我使用以下选项作为替代选项,为什么会出现错误: x = dataset.iloc[:, 0].values 这意味着,获取除最后一列之外的所有列: df = pd.DataFrame(np.random.randint(0,100,(5,5)), index=[*'abcde'], columns=[*'ABCDE'

我一直在学习Python线性回归教程:

使用以下数据集:

我的问题在于以下代码:

x = dataset.iloc[:, :-1].values
否定(-1)在这里做什么?如果我使用以下选项作为替代选项,为什么会出现错误:

x = dataset.iloc[:, 0].values

这意味着,获取除最后一列之外的所有列:

df = pd.DataFrame(np.random.randint(0,100,(5,5)), index=[*'abcde'], columns=[*'ABCDE'])

df.iloc[:,:-1]
输出:

    A   B   C   D
a  79  23   9  89
b  67  60  32  82
c  66  18  41  67
d  90  51  63  29
e  34  65  82  82
a    79
b    67
c    66
d    90
e    34
Name: A, dtype: int3
此语句获取所有行并对列进行切片以过滤掉最后一行。 而且,你的第二个陈述没有错误,这是一个好的陈述

df.iloc[:, 0]
输出:

    A   B   C   D
a  79  23   9  89
b  67  60  32  82
c  66  18  41  67
d  90  51  63  29
e  34  65  82  82
a    79
b    67
c    66
d    90
e    34
Name: A, dtype: int3

获取第一列的所有行(位置0)。

1表示位置。对于dataset.iloc[:,:-1]意味着获取除最后一列之外的所有列。