如何 link 具有序列索引的数组到具有 2 个键的数据帧?
How to link array with a serial index to a dataframe with 2 keys?
我在 Python 中有一个数据框,它具有名为 df_features
的数据集特征
University , Subject, F1, F2, F3, F4, F5, F6
Melb Math 4 3 9 2 5 5
Melb Physics 3 3 2 3 4 3
Melb English 6 7 6 8 7 3
Ottawa Math 4 7 8 7 2 7
Ottawa English 3 3 5 5 1 7
Ottawa Med 1 5 3 3 1 7
Syd Math 9 6 9 3 5 6
此数据框的键是 2 列
University , Subject
我使用这个数据集通过预先保存的模型预测值
loaded_model = pickle.load(open('E:\model.sav', 'rb'))
y_pred = loaded_model.predict(df_features)
现在y_pred是一个只有索引的数组
看起来像这样
0 Red
1 Blue
2 Green
3 Blue
4 Red
5 Red
6 Yellow
7 Red
8 Blue
如何将 y_pred 数组中的每个值与其在 df_features 中的大学和学科相匹配?
我想我找到了答案。
在数据框中添加一个新列并将其与数组匹配应该做到这一点
数组是dataframe的预测结果
因此,数据框和数组的索引顺序相似
df_features["Colour"] = y_pred
我在 Python 中有一个数据框,它具有名为 df_features
的数据集特征University , Subject, F1, F2, F3, F4, F5, F6
Melb Math 4 3 9 2 5 5
Melb Physics 3 3 2 3 4 3
Melb English 6 7 6 8 7 3
Ottawa Math 4 7 8 7 2 7
Ottawa English 3 3 5 5 1 7
Ottawa Med 1 5 3 3 1 7
Syd Math 9 6 9 3 5 6
此数据框的键是 2 列
University , Subject
我使用这个数据集通过预先保存的模型预测值
loaded_model = pickle.load(open('E:\model.sav', 'rb'))
y_pred = loaded_model.predict(df_features)
现在y_pred是一个只有索引的数组
看起来像这样
0 Red
1 Blue
2 Green
3 Blue
4 Red
5 Red
6 Yellow
7 Red
8 Blue
如何将 y_pred 数组中的每个值与其在 df_features 中的大学和学科相匹配?
我想我找到了答案。
在数据框中添加一个新列并将其与数组匹配应该做到这一点
数组是dataframe的预测结果
因此,数据框和数组的索引顺序相似
df_features["Colour"] = y_pred