如何在python pandas中合并2个复杂的数据框?

我有2个熊猫数据帧.

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中列组的匹配. df2中的列组都是唯一值.

我需要以下列方式组合上述2个数据帧:

           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

我知道我已经在熊猫问过类似的问题了.我是大熊猫的新手,所以如果我问这样的问题,请耐心等待.

最佳答案 以下将有效:

d_teams=pd.DataFrame( [[int(y) for y in x.split('_')[1:]] \
            for x in df1.match_up], columns=('team_A', 'team_B') )
merged=pd.concat((df1,d_teams),axis=1)
df2i=df2.set_index('team')
merged['win_A']=df2i.ix[merged.team_A].reset_index().win_A_B
merged['win_B']=df2i.ix[merged.team_B].reset_index().win_A_B

首先,我们创建d_teams,它是一个由match_up列组成的DataFrame,由’_’拆分,并转换为int.我们扔掉了一年,因为它已经包含在df1中,只是保留了team_A和team_B.然后我们通过将其与df1连接来创建合并的数据帧.

接下来,我们创建df2i,它是由团队索引的df2.然后我们可以使用merged.team_A或merged.team_B进行索引以获取获胜值.但是,我们不希望结果由这些团队编制索引,因此我们首先重置索引.

点赞