Search code examples
pythonpandasregressioncoefficients

Is there a way to display regression coefficients in a pandas data frame for categorical independent variables?


I have built a multiple linear regression model and I found the coefficients using model.coef_. I want to make a pandas data frame which displays each of the factors and its coefficient.

pd.DataFrame(model.coef_, x.columns, columns = ['coef']).sort_values(by = 'coef', ascending = False) works only for the numerical independent variables. I have two columns of categorical variables and I have encoded them both.

Suppose the two values of a column are 'male' and 'female', I want to display the individual coefficients like

coef
Male 0.2
Female 0.3

Is it possible to do this?


Solution

  • You can set_index after

    (pd.DataFrame({'coef':model.coef_, 'category':x.columns})
    .sort_values(by = 'coef', ascending = False)
    .set_index('category'))