简体   繁体   English

基于匹配的列组合行熊猫

[英]Combining rows based on matching columns pandas

I have a csv file containing games and stats for each team for an entire season.我有一个 csv 文件,其中包含整个赛季每支球队的比赛和统计数据。 I am wanting to move the away team into the same row as the home team it faced for that week.我想将客队与那周面对的主队放在同一排。

Current dataframe:当前数据框:

      Week   Team   H/a   Opp   Pf   Pa   Pyards  
      1      A            C     3    14   100     
      1      B            D     7    21   200     
      1      C      @     A     14   3    300     
      1      D      @     B     21   7    400     

Desired dataframe:所需的数据帧:

  Week   HomeTeam   H-score   H-Pyards   AwayTeam   A-score   A-Pyards  
  1      A          3         100        C          14        300       
  1      B          7         200        D          21        400       

But I would have more stats for each team and mutliple weeks.但是我会为每个团队和多个周提供更多统计数据。

I believe the operation you are looking is self-join with some manipulation afterwards.我相信你正在寻找的操作是self-join之后进行一些操作。 As Quang Hoang stated, merging the same dataframe/table in different columns is called self-join.正如 Quang Hoang 所说,在不同的列中合并相同的数据帧/表称为自联接。 I believe this is an approach which gets the expected output:我相信这是一种获得预期输出的方法:

df = pd.DataFrame({'Week':[1,1,1,1],
                   'Team':['A','B','C','D'],
                   'H/a':[np.nan,np.nan,'@','@'],
                   'Opp':['C','D','A','B'],
                   'Pf':[3,7,14,21],
                   'Pa':[14,21,3,7],
                   'Pyards':[100,200,300,400]})
print(df)
new_df = df.merge(df,how='inner',left_on=['Week','Team'],right_on=['Week','Opp'])
new_df = new_df[new_df['H/a_x'] != '@']
replacers = {'Team_x':'HomeTeam','Pf_x':'Pf','Pyards_x':'H-Pyards','Opp_x':'AwayTeam','Pa_x':'A-score','Pyards_y':'A-Pyards'}
new_df = new_df[['Week']+[x for x in replacers.keys()]]
new_df = new_df.rename(columns=replacers)
print(new_df)

Output:输出:

   Week HomeTeam  Pf  H-Pyards AwayTeam  A-score  A-Pyards
0     1        A   3       100        C       14       300
1     1        B   7       200        D       21       400

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM