我有两个pandas数据框。
dictionary1 = {'match_up' : ['1985_1116_1234' , '1985_1116_1475', '1985_1234_1172', '1985_1475_2132', '1985_1242_1325'], \
'result': [1, 1, 0, 0, 1], 'year':[1985,1985,1985,1985,1985] }
dictionary2 = {'team' : [1234 , 1475, 2132, 1172, 1242, 1116 , 1325], 'win_A_B': [0.667, 0.636, 0.621, 0.629, 0.615,0.943, 0.763], \
'year':[1985,1985,1985,1985,1985,1985,1985] }
df1 = pd.DataFrame(dictionary1)
df2 = pd.DataFrame(dictionary2)
df1:
match_up result year
0 1985_1116_1234 1 1985
1 1985_1116_1475 1 1985
2 1985_1234_1172 0 1985
3 1985_1475_2132 0 1985
4 1985_1242_1325 1 1985
df2:
team win_A_B year
1234 0.667 1985
1475 0.636 1985
2132 0.621 1985
1172 0.629 1985
1242 0.615 1985
1116 0.943 1985
1325 0.763 1985
数据框df1中的列值与数据框df2中的列team相匹配。数据框df2中的列team的值都是唯一的。
我需要按以下方式组合上述两个数据框:
match_up result year team_A team_B win_A win_B
0 1985_1116_1234 1 1985 1116 1234 0.943 0.667
1 1985_1116_1475 1 1985 1116 1475 0.943 0.636
2 1985_1234_1172 0 1985 1234 1172 0.667 0.629
3 1985_1475_2132 0 1985 1475 2132 0.636 0.621
4 1985_1242_1325 1 1985 1242 1325 0.615 0.763
我知道我之前在pandas中已经问过类似的问题。我是pandas的新手,如果我问了一些这样的问题,请您耐心等待。