Formulas

smf.ols("y ~ x", df).

smf.ols('y ~ x', df) reads like a sentence. C(city) is a dummy.

Goal

Fit shillings ~ units + C(city).

import pandas as pd
import statsmodels.formula.api as smf
df = pd.DataFrame({'units': [12, 9, 3, 7, 11, 4], 'city': ['Nairobi', 'Mombasa', 'Kisumu', 'Nairobi', 'Mombasa', 'Kisumu'], 'shillings': [126, 198, 93, 73.5, 242, 124]})
fit = smf.ols('shillings ~ units + C(city)', data=df).fit()
print(fit.params.round(3).to_dict())
import pandas as pd
import statsmodels.formula.api as smf
df = pd.DataFrame({'x': [1, 2, 3, 4], 'y': [1, 4, 9, 16]})
print(smf.ols('y ~ x + I(x**2)', data=df).fit().params.round(3).to_dict())
import pandas as pd
import statsmodels.formula.api as smf
df = pd.DataFrame({'units': [12, 9, 3], 'shillings': [126, 198, 93]})
print(smf.ols('shillings ~ units', data=df).fit().rsquared)
import pandas as pd
import statsmodels.formula.api as smf
df = pd.DataFrame({'a': [1, 2, 3], 'b': [2, 2, 4], 'y': [3, 5, 8]})
print(smf.ols('y ~ a + b', data=df).fit().params.round(3).to_dict())