How to make the variable "X" I can assign specific columns, as I did with the variable "Y". When trying to do this for some reason, it gives a list of rows 1 through n and not the data of the columns.

import pandas as pd pd.options.mode.chained_assignment = None names = ['sepal length','sepal width','petal length','petal width','class'] dataframe = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data", names = names) from sklearn.cross_validation import train_test_split dataframe['class'].replace("Iris-setosa",1,inplace= True) dataframe['class'].replace("Iris-virginica",2,inplace = True) dataframe['class'].replace("Iris-versicolor",3,inplace=True) X = dataframe[1:3] Y = dataframe["class"] X.head() Out[90]: sepal length sepal width petal length petal width class 1 4.9 3.0 1.4 0.2 1 2 4.7 3.2 1.3 0.2 1 

1 answer 1

dataframe[1:3] - displays lines with indices: 1:3

 In [8]: dataframe[1:3] Out[8]: sepal length sepal width petal length petal width class 1 4.9 3.0 1.4 0.2 Iris-setosa 2 4.7 3.2 1.3 0.2 Iris-setosa 

dataframe.iloc[:, 1:3] - show columns with indices from 1 to 2 :

 In [9]: dataframe.iloc[:, 1:3] Out[9]: sepal width petal length 0 3.5 1.4 1 3.0 1.4 2 3.2 1.3 3 3.1 1.5 4 3.6 1.4 5 3.9 1.7 6 3.4 1.4 7 3.4 1.5 8 2.9 1.4 9 3.1 1.5 .. ... ... 140 3.1 5.6 141 3.1 5.1 142 2.7 5.1 143 3.2 5.9 144 3.3 5.7 145 3.0 5.2 146 2.5 5.0 147 3.0 5.2 148 3.4 5.4 149 3.0 5.1 [150 rows x 2 columns] 

dataframe.iloc[:, 1:3] - show columns with indices from 1 to 3 :

 In [10]: dataframe.iloc[:, 1:4] Out[10]: sepal width petal length petal width 0 3.5 1.4 0.2 1 3.0 1.4 0.2 2 3.2 1.3 0.2 3 3.1 1.5 0.2 4 3.6 1.4 0.2 5 3.9 1.7 0.4 6 3.4 1.4 0.3 7 3.4 1.5 0.2 8 2.9 1.4 0.2 9 3.1 1.5 0.1 .. ... ... ... 140 3.1 5.6 2.4 141 3.1 5.1 2.3 142 2.7 5.1 1.9 143 3.2 5.9 2.3 144 3.3 5.7 2.5 145 3.0 5.2 2.3 146 2.5 5.0 1.9 147 3.0 5.2 2.0 148 3.4 5.4 2.3 149 3.0 5.1 1.8 [150 rows x 3 columns]