How to split data frame into x and y

Question:

I am splitting the data into training data and testing data like so:

train, test = train_test_split(dataFrame(), test_size=0.2)

Which works wonders, my training data frame looks like this:

     PassengerId  Survived  SibSp  Parch
77            78         0      0      0
748          749         0      1      0
444          445         1      0      0
361          362         0      1      0
576          577         1      0      0
27            28         0      3      2
232          233         0      0      0
424          425         0      1      1
785          786         0      0      0
…            …           …      …      … 

I am now attempting to get the X and Y columns, X being my SibSp column and Y being my Parch column. After following many tutorials on Regression and training my AI, every person “split” the columns into x and y like so:

x = train[:, 0:2]

However, after many variations and googling, I cannot solve this error this line is giving me nor understand it:

TypeError: unhashable type: ‘slice’

How can I split the SibSp column into an array of x and the Parch column into an array of y within my training data frame?

Asked By: Danny_P

||

Answers:

The correct way to slice is x = train.iloc[:, 0:2].

Answered By: Xiaoyu Lu

If your target class is the last column, the most generic solution is:

X = df.iloc[:, 0:-1]
y = df.iloc[:, -1]
Answered By: Sorul
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.