Ber*_*ans 4 python-2.7 pandas dummy-variable
I have a DataFrame with an ID variable and another categorical variable. I want to create dummy variables out of the categorical variable with get_dummies.
dum = pd.get_dummies(df)
Run Code Online (Sandbox Code Playgroud)
However, this makes the ID variable disappear. And I need this ID variable later on to merge to other data sets.
Is there a way to keep other variables. In the documentation of get_dummies I could not find anything. Thanks!
您还可以在执行 get_dummies 之前将原始列复制到新列中。例如,
df['dum_orig'] = df['dum']
df = pd.get_dummies(df, columns=['dum'])
Run Code Online (Sandbox Code Playgroud)
我找到了答案。您可以将虚拟数据集连接到原始数据集,如下所示。只要您在此期间不重新排序数据即可。
df = pd.concat([df, dum], axis=1)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7410 次 |
| 最近记录: |