在Python中转换和附加数据帧

2024-09-23 22:29:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的数据帧称为“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

Tags: 数据dfdatevaluearsenaldfuhometeamawayteam
1条回答
网友
1楼 · 发布于 2024-09-23 22:29:06

Merge ^{} with ^{}两次1

import pandas as pd
df_1 = pd.DataFrame({'Date': ['8/14/1993', '8/14/1993', '8/14/1993'],
                    'HomeTeam': ['Arsenal', 'Aston Villa', 'Chelsea'],
                    'AwayTeam': ['Coventry', 'QPR', 'Blackburn']})
df_2 = pd.DataFrame({'Team': ['Arsenal', 'QPR', 'Blackburn', 'Coventry', 'Chelsea', 'Aston Villa'],
                    'Game N.': [0, 1, 2, 0, 2, 1],
                    'Value': [-1, 2, 4, 3, 0, -2]})
df_1 = df_1.reset_index()

result = (df_1.merge(df_2.rename(columns={'Team':'HomeTeam', 'Game N.':'index','Value':'Value_Home'}))
             .merge(df_2.rename(columns={'Team':'AwayTeam', 'Game N.':'index','Value':'Value_Away'})))
result = result.drop('index', axis=1)
print(result)

收益率

        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

1 默认情况下,merge合并两个数据帧共享的所有列名。所以窍门是重命名df_2的列,这样合并就发生在适当的列上。 例如,给定df_1df_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

我们希望将df_1indexHomeTeam列与df_2Game N.Team列合并。 因此,如果我们将df_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_Away列可以用同样的方法获得

相关问题 更多 >