I have a pandas data frame and I would like to able to predict the values of column A from the values in columns B and C. Here is a toy example:

```import pandas as pd
df = pd.DataFrame({"A": [10,20,30,40,50],
"B": [20, 30, 10, 40, 50],
"C": [32, 234, 23, 23, 42523]})
```

Ideally, I would have something like ols(A ~ B + C, data = df) but when I look at the examples from algorithm libraries like scikit-learn it appears to feed the data to the model with a list of rows instead of columns. This would require me to reformat the data into lists inside lists, which seems to defeat the purpose of using pandas in the first place. What is the most pythonic way to run an OLS regression (or any machine learning algorithm more generally) on data in a pandas data frame?

Mar 26, 2022 3,747 views

## 1 answer to this question.

I believe you may almost achieve your desired result by utilising the statsmodels package, which was one of pandas' optional dependencies prior to version 0.20.0. (it was used for a few things in pandas.stats.)

```import pandas as pd
import statsmodels.formula.api as sm
data = pd.DataFrame({"A": [10,20,30,40,50], "B": [20, 30, 10, 40, 50], "C": [32, 234, 23, 23, 42523]})
result = sm.ols(formula="A ~ B + C", data).fit()
print(result.params)
Intercept    14.952480
B             0.401182
C             0.000352
dtype: float64```
``` print(result.summary())
OLS Regression Results
==============================================================================
Dep. Variable:                      A   R-squared:                       0.579
Method:                 Least Squares   F-statistic:                     1.375
Date:                Thu, 14 Nov 2013   Prob (F-statistic):              0.421
Time:                        20:04:30   Log-Likelihood:                -18.178
No. Observations:                   5   AIC:                             42.36
Df Residuals:                       2   BIC:                             41.19
Df Model:                           2
==============================================================================
coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
Intercept     14.9525     17.764      0.842      0.489       -61.481    91.386
B              0.4012      0.650      0.617      0.600        -2.394     3.197
C              0.0004      0.001      0.650      0.583        -0.002     0.003
==============================================================================
Omnibus:                          nan   Durbin-Watson:                   1.061
Prob(Omnibus):                    nan   Jarque-Bera (JB):                0.498
Skew:                          -0.123   Prob(JB):                        0.780
Kurtosis:                       1.474   Cond. No.                     5.21e+04
==============================================================================

Warnings:
[1] The condition number is large, 5.21e+04. This might indicate that there are
strong multicollinearity or other numerical problems.```

Supercharge Your Skills with Our Machine Learning Course!

• 5,480 points

## Can I draw a regression line and show parameters using scatterplot with a pandas dataframe?

There isn't a parameter for DataFrame.plot(). Seaborn, ...READ MORE

## Logistic Regression with continuous data using sklearn in python

Despite the fact that it produces a ...READ MORE

## Example of Logistic regression with python code

Have a look at this: import csv import ...READ MORE

## how do i change string to a list?

suppose you have a string with a ...READ MORE

## how can i randomly select items from a list?

You can also use the random library's ...READ MORE

+1 vote

## how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE