I have a Pandas dataframe with 10 columns, 9 of which are features to be used to predict the 10th column.

How is it ossible to convert this Pandas dataframe into X and y vectors to use in a linear regression problem?


2 Answers 2


If you have your dataframe loaded as the variable df, you can simply use this

X = df[['A','B','C']]
y = df['Z']

where A, B and C are your independent variables and Z is your dependent variable.

  • $\begingroup$ is that possible to use X and y for train_test_split further? $\endgroup$
    – MJay
    Sep 2, 2019 at 7:39
  • $\begingroup$ Yes, it is possible. Please see sklearn's documentation for more details . Have a look here scikit-learn.org/stable/modules/generated/… $\endgroup$ Sep 2, 2019 at 7:41

Are you looking for this?

#format the data as a numpy array to feed into the algorithm
X = np.asarray([np.asarray(df['Ind1']),np.asarray(df['Ind2']),np.asarray(df['Ind3'])])
y = np.asarray([np.asarray(df['Dep'])])

Or, simplified.

# array(['a', 'b', 'c'], dtype=object)
arr = df.index.to_numpy()

#  array([1, 2, 3])
arr = df['A'].to_numpy()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.