Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.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中转换和附加数据帧_Python_Dataframe - Fatal编程技术网

在Python中转换和附加数据帧

在Python中转换和附加数据帧,python,dataframe,Python,Dataframe,我有一个名为“df_1”的数据帧 我还有一个名为“df_2”的数据帧 df_2中“游戏N”列中的数字对应于df_1中索引的数字。两个表中的团队名称也相同 是否可以通过创建两个名为value_Home和value_Away的新列,将df_2中“value”列中的每个值分配(追加)到df_1中相应的行(索引)。这就是我试图实现的输出: Date HomeTeam AwayTeam Value_Home Value_Away 0 8/14/1993 Arse

我有一个名为“df_1”的数据帧

我还有一个名为“df_2”的数据帧

df_2中“游戏N”列中的数字对应于df_1中索引的数字。两个表中的团队名称也相同

是否可以通过创建两个名为value_Home和value_Away的新列,将df_2中“value”列中的每个值分配(追加)到df_1中相应的行(索引)。这就是我试图实现的输出:

      Date     HomeTeam     AwayTeam    Value_Home  Value_Away
 0  8/14/1993   Arsenal     Coventry        -1           3
 1  8/14/1993   Aston Villa    QPR          -2           2
 2  8/14/1993   Chelsea     Blackburn        0           4
twice1:

屈服

        Date     HomeTeam   AwayTeam  Value_Home  Value_Away
0  8/14/1993      Arsenal   Coventry          -1           3
1  8/14/1993  Aston Villa        QPR          -2           2
2  8/14/1993      Chelsea  Blackburn           0           4

一, 默认情况下,
merge
合并两个数据帧共享的所有列名。因此,诀窍是重命名
df_2
的列,以便在适当的列上进行合并。 例如,给定如下
df_1
df_2

In [39]: df_1
Out[39]: 
   index       Date     HomeTeam   AwayTeam
0      0  8/14/1993      Arsenal   Coventry
1      1  8/14/1993  Aston Villa        QPR
2      2  8/14/1993      Chelsea  Blackburn

In [40]: df_2
Out[40]: 
          Team  Game N.  Value
0      Arsenal        0     -1
1          QPR        1      2
2    Blackburn        2      4
3     Coventry        0      3
4      Chelsea        2      0
5  Aston Villa        1     -2
In [31]: df_2.rename(columns={'Team':'HomeTeam', 'Game N.':'index','Value':'Value_Home'})
Out[36]: 
      HomeTeam  index  Value_Home
0      Arsenal      0          -1
1          QPR      1           2
2    Blackburn      2           4
3     Coventry      0           3
4      Chelsea      2           0
5  Aston Villa      1          -2
我们希望将
dfu 1
索引
主队
列与
dfu 2
游戏编号
球队
列合并。 因此,如果我们像这样重命名
df_2
的列:

In [39]: df_1
Out[39]: 
   index       Date     HomeTeam   AwayTeam
0      0  8/14/1993      Arsenal   Coventry
1      1  8/14/1993  Aston Villa        QPR
2      2  8/14/1993      Chelsea  Blackburn

In [40]: df_2
Out[40]: 
          Team  Game N.  Value
0      Arsenal        0     -1
1          QPR        1      2
2    Blackburn        2      4
3     Coventry        0      3
4      Chelsea        2      0
5  Aston Villa        1     -2
In [31]: df_2.rename(columns={'Team':'HomeTeam', 'Game N.':'index','Value':'Value_Home'})
Out[36]: 
      HomeTeam  index  Value_Home
0      Arsenal      0          -1
1          QPR      1           2
2    Blackburn      2           4
3     Coventry      0           3
4      Chelsea      2           0
5  Aston Villa      1          -2
然后合并两个数据帧产生

In [38]: df_1.merge(df_2.rename(columns={'Team':'HomeTeam', 'Game N.':'index','Value':'Value_Home'}))
Out[38]: 
   index       Date     HomeTeam   AwayTeam  Value_Home
0      0  8/14/1993      Arsenal   Coventry          -1
1      1  8/14/1993  Aston Villa        QPR          -2
2      2  8/14/1993      Chelsea  Blackburn           0
Value\u Away
列也可以用同样的方法获得

In [38]: df_1.merge(df_2.rename(columns={'Team':'HomeTeam', 'Game N.':'index','Value':'Value_Home'}))
Out[38]: 
   index       Date     HomeTeam   AwayTeam  Value_Home
0      0  8/14/1993      Arsenal   Coventry          -1
1      1  8/14/1993  Aston Villa        QPR          -2
2      2  8/14/1993      Chelsea  Blackburn           0