Currently I'm learning data science and I'm in the beginners stage. I have seen many times we add a "constant" column in our data frame with all row cells of that column having value 1.

I need to know why we do so. And also what will happen if we don't use it.

Thank you.

  • $\begingroup$ Maybe as the reference column for one hot encoding of a categorical feature? This column is typically dropped though. Personally, I have never heard of anyone intentionally including a zero variance predictor into their model. That cannot provide any useful information in predicting your response (zero information with a constant variable) and can lead to numerical problems for some linear models. $\endgroup$
    – aranglol
    Jul 13, 2019 at 5:20
  • $\begingroup$ Yes. Even I think adding this is of no use. But I think there could be some error if we don't use it. Is it so? $\endgroup$ Jul 13, 2019 at 5:27
  • $\begingroup$ I mean, what error could there be? In a one hot encoding example the constant column is dropped because we can infer the status of the last category through the other columns (you probably know this already). Are there any specific examples you can give in which this column is added? $\endgroup$
    – aranglol
    Jul 13, 2019 at 5:35
  • $\begingroup$ Suppose, there is a DataFrame with columns GRE, GPA, Rank, Admission_Possibility ..here GRE is insignificant according to data and so we drop the GRE column and add a constant column to the DataFrame. $\endgroup$ Jul 13, 2019 at 5:55
  • $\begingroup$ That seems very suspect to me. What do you mean by insignificant? As in a statistical test in linear regression? Besides using p values for feature selection which is ill advised, most packages just drop any constant columns from the data frame because they introduce numerical problems (singular matrices). I am not too sure what the purpose of adding a constant column would do in your example or even why you would do that in the first place. Perhaps to keep the data frame in the same shape for convenience? Setting a column to all 1's that previously had a non constant variable... $\endgroup$
    – aranglol
    Jul 13, 2019 at 19:10

1 Answer 1


In linear regression you need that column to have lines which are not constrained to pass through origin. Think of linear model $y = b_1 x_1 + b_2 x_2 + ...$. Iff all $x_i$ are 0, y must be 0, you need an additional parameter to pass that constraint.

  • $\begingroup$ This is a reference to the design matrix X in which the constant column of 1's is set for the intercept commonly referred to as b_0 right? But this is in general always added to the matrix by default unless you explicitly state to have no intercept in the function call so I am unsure if this is what the OP means. I am not your downvoter by the way. $\endgroup$
    – aranglol
    Jul 13, 2019 at 15:34
  • $\begingroup$ +1: This is required in some packages (statsmodels in particular), and is the only time I've seen this done. $\endgroup$
    – Ben Reiniger
    Jul 13, 2019 at 21:34

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.