我有下面的数据帧称为“dfu 1”
Date HomeTeam AwayTeam
0 8/14/1993 Arsenal Coventry
1 8/14/1993 Aston Villa QPR
2 8/14/1993 Chelsea Blackburn
我还有一个名为“dfè2”的数据帧
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
dfu 2中“Game N.”列中的数字与dfu 1中索引的数字相对应。而且两个表中的球队名称相同
是否可以通过创建两个名为value\u Home和value\u Away的新列,将df\u 2中“value”列中的每个值赋(附加)到df\u 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
Merge ^{} with ^{} 两次1:
收益率
1 默认情况下,
merge
合并两个数据帧共享的所有列名。所以窍门是重命名df_2
的列,这样合并就发生在适当的列上。 例如,给定df_1
和df_2
如下:我们希望将
df_1
的index
和HomeTeam
列与df_2
的Game N.
和Team
列合并。 因此,如果我们将df_2
的列重命名为:然后合并两个数据帧产生
Value_Away
列可以用同样的方法获得相关问题 更多 >
编程相关推荐