1

我有一个这样的数据框:

Index ID  Industry  years_spend       asset
6646  892         4            4  144.977037
2347  315        10            8  137.749138
7342  985         1            5  104.310217
137    18         5            5  156.593396
2840  381        11            2  229.538828
6579  883        11            1  171.380125
1776  235         4            7  217.734377
2691  361         1            2  148.865341
815   110        15            4  233.309491
2932  393        17            5  187.281724

我想为 Industry X years_spend 创建虚拟变量,该变量创建变量len(df.Industry.value_counts()) * len(df.years_spend.value_counts()),例如 d_11_4 = 1 用于具有行业 ==1 和年花费 =4 的所有行,否则为 d_11_4 = 0。然后我可以将这些变量用于一些回归工作。

我知道我可以使用 df.groupby(['Industry','years_spend']) 制作我想要的组,并且我知道我可以使用以下patsy语法为一列创建这样的变量statsmodels

import statsmodels.formula.api as smf

mod = smf.ols("income ~   C(Industry)", data=df).fit()

但如果我想处理 2 列,我会收到一个错误: IndexError: tuple index out of range

如何使用 pandas 或使用 statsmodels 中的某些功能来做到这一点?

4

2 回答 2

4

使用 patsy 语法它只是:

import statsmodels.formula.api as smf

mod = smf.ols("income ~ C(Industry):C(years_spend)", data=df).fit()

字符的:意思是“互动”;您还可以将其推广到两个以上项目的交互 ( C(a):C(b):C(c))、数值和分类值之间的交互等。您可能会发现patsy 文档很有用

于 2017-07-14T04:00:22.127 回答
3

你可以做这样的事情,你必须首先创建一个封装Industryand的计算字段years_spend

df = pd.DataFrame({'Industry': [4, 3, 11, 4, 1, 1], 'years_spend': [4, 5, 8, 4, 4, 1]})
df['industry_years'] = df['Industry'].astype('str') + '_' + df['years_spend'].astype('str')  # this is the calculated field

外观如下df

   Industry  years_spend industry_years
0         4            4            4_4
1         3            5            3_5
2        11            8           11_8
3         4            4            4_4
4         1            4            1_4
5         1            1            1_1

现在您可以申请get_dummies

df = pd.get_dummies(df, columns=['industry_years'])

这会让你得到你想要的:)

于 2017-07-12T12:40:47.503 回答