将迭代打印结果转换为pandas Dataframe

i5desfxk  于 2023-05-12  发布在  其他
关注(0)|答案(1)|浏览(169)

如何从多列迭代的打印结果创建 Dataframe ?有什么参考资料吗?谢谢

for i in range(2):
      test = regression.linear_model.OLS(df[['s'+str(i+1)]],sm.add_constant(df[['benchmark']])).fit()
      print(test.params)
      print(test.tvalues)
      print(test.pvalues)

输出:

const        0.018959
benchmark    0.770473
dtype: float64
const        3.586451
benchmark    8.573976
dtype: float64
const        4.329121e-04
benchmark    4.732058e-15
dtype: float64
const        0.018192
benchmark    0.778906
dtype: float64
const        3.180102
benchmark    8.009541
dtype: float64
const        1.736846e-03
benchmark    1.450519e-13
dtype: float64
whlutmcx

whlutmcx1#

您可以像下面这样手动地将它们组织到一个pandas dataframe中(并且可以随意命名这些列)

pd_results= pd.DataFrame({"ols_params": result.params, "ols_tvalues": result.tvalues, "ols_pvalues": result.pvalues})

我将从这里的statsmodel示例开始,这样我就有了要处理的数据(https://www.statsmodels.org/devel/generated/statsmodels.regression.linear_model.OLS.html),并显式地使用linear_model

import statsmodels.api as sm
import statsmodels.regression.linear_model as linear_model
import numpy as np
# sm dataset
duncan_prestige = sm.datasets.get_rdataset("Duncan", "carData")
Y = duncan_prestige.data['income']
X = duncan_prestige.data['education']
X = sm.add_constant(X)
# linear_model OLD
model = linear_model.OLS(Y,X)
results = model.fit()
results.params
pd_results = pd.DataFrame({"ols_params": results.params, "ols_tvalues": results.tvalues, "ols_pvalues": results.pvalues})
pd_results

下面是它在collab中的样子:

相关问题