Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

python - 如何使用DictVectorizer进行转换和反演后检索原始数据帧(How to retrieve the original data frame after transforming and inversing using DictVectorizer)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...