Problem: I have a categorical data frame with more than 10 columns, below is just a sample data.
(问题:我有一个类别数据框,其中有10列以上,下面只是示例数据。)
I want to transform and retrieve back the original data frame but I get column levels split into multiple columns. (我想转换并检索回原始数据框,但是我将列级别分为多个列。)
Reason: From the model I would want to append the predicted results back to the test data set.
(原因:从模型中,我想将预测结果附加到测试数据集。)
I have tried get_dummies but was unable to retrieve it back. (我尝试过get_dummies,但无法将其取回。)
df.head()
v = DictVectorizer(sparse=False)
D=df.to_dict(orient='records')
X=v.fit_transform(D)
n=v.inverse_transform(X)
n #here I get the region column split into multiple columns
[{'Region=East': 1.0, 'Units': 95.0},
{'Region=Central': 1.0, 'Units': 50.0},
{'Region=Central': 1.0, 'Units': 36.0},
{'Region=Central': 1.0, 'Units': 27.0},
{'Region=West': 1.0, 'Units': 56.0},
{'Region=East': 1.0, 'Units': 60.0}]
pd.DataFrame.from_dict(n)
How can I get it back to the original form.
(我如何将其恢复为原始形式。)
ask by Shuhom Choudhury translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…