如何使用其中一列作为参考来匹配两列?
how to match two columns using one of the column as reference?
我已经做了一些分析并找到了一个特定的模式,现在我正在尝试做一些预测。
我有一个数据集,可以预测在童年时期发生过一定数量事故的学生的评分。
我的预测矩阵看起来像这样:
A
injuries ratings
0 5
1 4.89
2 4.34
3 3.99
4 3.89
5 3.77
我的数据集如下所示:
B
siblings income injuries total_scoldings_from father
3 12000 4 09
4 34000 5 22
1 23400 3 12
3 24330 1 1
0 12000 1 12
现在我想创建一个列名 predictions,它基本上匹配从 A
到 B
和 returns [=15] 的条目=]
siblings income injuries total_scoldings_from_father predictions
3 12000 4 09 3.89
4 34000 5 22 3.77
1 23400 3 12 3.99
3 24330 1 1 4.89
0 12000 1 12 4.89
请帮忙
另外建议一个标题,因为我的缺少对未来参考重要的一切
如果映射的所有值都在 DataFrame 中,您可以使用 map
A
:
B['predictions'] = B['injuries'].map(A.set_index('injuries')['ratings'])
print (B)
siblings income injuries total_scoldings_from_father predictions
0 3 12000 4 9 3.89
1 4 34000 5 22 3.77
2 1 23400 3 12 3.99
3 3 24330 1 1 4.89
4 0 12000 1 12 4.89
merge
的另一个解决方案:
C = pd.merge(B,A)
print (C)
siblings income injuries total_scoldings_from_father ratings
0 3 12000 4 9 3.89
1 4 34000 5 22 3.77
2 1 23400 3 12 3.99
3 3 24330 1 1 4.89
4 0 12000 1 12 4.89
我已经做了一些分析并找到了一个特定的模式,现在我正在尝试做一些预测。 我有一个数据集,可以预测在童年时期发生过一定数量事故的学生的评分。 我的预测矩阵看起来像这样:
A
injuries ratings
0 5
1 4.89
2 4.34
3 3.99
4 3.89
5 3.77
我的数据集如下所示:
B
siblings income injuries total_scoldings_from father
3 12000 4 09
4 34000 5 22
1 23400 3 12
3 24330 1 1
0 12000 1 12
现在我想创建一个列名 predictions,它基本上匹配从 A
到 B
和 returns [=15] 的条目=]
siblings income injuries total_scoldings_from_father predictions
3 12000 4 09 3.89
4 34000 5 22 3.77
1 23400 3 12 3.99
3 24330 1 1 4.89
0 12000 1 12 4.89
请帮忙
另外建议一个标题,因为我的缺少对未来参考重要的一切
如果映射的所有值都在 DataFrame 中,您可以使用 map
A
:
B['predictions'] = B['injuries'].map(A.set_index('injuries')['ratings'])
print (B)
siblings income injuries total_scoldings_from_father predictions
0 3 12000 4 9 3.89
1 4 34000 5 22 3.77
2 1 23400 3 12 3.99
3 3 24330 1 1 4.89
4 0 12000 1 12 4.89
merge
的另一个解决方案:
C = pd.merge(B,A)
print (C)
siblings income injuries total_scoldings_from_father ratings
0 3 12000 4 9 3.89
1 4 34000 5 22 3.77
2 1 23400 3 12 3.99
3 3 24330 1 1 4.89
4 0 12000 1 12 4.89