Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中合并2个复杂的数据帧?_Python_Python 2.7_Pandas - Fatal编程技术网

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

如何在python中合并2个复杂的数据帧?,python,python-2.7,pandas,Python,Python 2.7,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,

我有两个数据帧

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

我知道我已经在熊猫身上问过类似的问题。我是熊猫的新手,所以如果我问这样的问题,请耐心听我说。

以下几点可行:

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列组成,按“u”分割,并转换为int。我们扔掉这一年,因为它已经包含在df1中,只保留team_A和team_B。然后我们通过将其与df1连接起来创建一个合并的数据帧


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

如果您以简单的形式提供数据供我们导入,这将非常有用。@cge好的,让我编辑我的问题。@cge我已经编辑了问题。您能解释一下
merged['win_A']=df2i.ix[merged.team_A].reset_index()吗.win_A_B
此命令?
已合并。team_A
已合并
中的
team_A
值列表。由于我们已经将团队值作为
df2i
的索引,
df2i.ix[merged.team_A]
为我们提供了一个数据帧,其中包含来自
df2i
的行,这些行对应于
team_A
值。然后我们用
reset_index()
删除团队索引,并从该数据框中选择
win_A_B
列。然后我们将其分配到
合并的
中的
赢A
列。是的,我得到了它。惊人的解释!!一个简单的查询->
[[x.split中y的int(y)](“u')[1:][df1中x的int(y)]匹配]
给出了以下输出:
[[11161234]、[11161475]、[12341172]、[14752132]、[1242132]
。现在如何传递这个
d_teams=pd.DataFrame([[int(y)for y in x.split('''u')[1:]\ for x in df1.match_up],columns=('team_A','team_B'))
将我的内部列表中的第一个值指定为第一列,将第二个值指定为第二列。在字典中,我知道键变为我的列,值变为我的行。是否有与此列表相关的类似概念?