简体   繁体   English

在 Pandas 中键不相等的地方加入

[英]Join where keys are not equal in Pandas

I have a dataframe like this:我有一个像这样的 dataframe:

data = {'teamid': [1, 2, 3, 4], 'gameid': [1, 1, 2, 2], 'rebounds': [20, 35, 43, 15]}
game_df = pd.DataFrame(data=data)
print(game_df)

   teamid  gameid  rebounds
0       1       1        20
1       2       1        35
2       3       2        43
3       4       2        15

I would like to join it to it self to produce a dataframe like this:我想自己加入它来产生这样的 dataframe:

wanted_data = {'teamid': [1, 2, 3, 4], 'gameid': [1, 1, 2, 2], 'rebounds': [20, 35, 43, 15],
               'teamid_opponent': [2, 1, 4, 3], 'rebound_opponent': [35, 20, 15, 43]}
wanted_df = pd.DataFrame(data=wanted_data)
print(wanted_df)

   teamid  gameid  rebounds  teamid_opponent  rebound_opponent
0       1       1        20                2                35
1       2       1        35                1                20
2       3       2        43                4                15
3       4       2        15                3                43

In SQL I would just do something like this:在 SQL 我会做这样的事情:

SELECT * from game_df df1 join game_df df2 on df1.gameid = df2.gameid and df1.teamid != df2.teamid

But i haven't been able to find anything in the pandas docs or on here for a way to replicate this in pandas itself.但我无法在 pandas 文档中或在此处找到任何在 pandas 本身中复制此内容的方法。 I looked on here and found this link but it isn't quite the same as what I'm trying to do.我在这里查看并找到了这个链接,但它与我正在尝试做的不太一样。 I've only found examples of trying to join where keys are equal.我只找到了尝试在键相等的地方加入的示例。

Here is one way use merge这是使用merge的一种方法

Yourdf=game_df.merge(game_df,on='gameid',suffixes =['','_opponent']).query('teamid!=teamid_opponent')
Out[42]: 
   teamid  gameid  rebounds  teamid_opponent  rebounds_opponent
1       1       1        20                2                 35
2       2       1        35                1                 20
5       3       2        43                4                 15
6       4       2        15                3                 43

I will also add seeing the answer made me think of a different way to do this as well so I will post that for posterity我还会补充说,看到答案让我也想到了一种不同的方法来做到这一点,所以我会把它发布给后代

Yourdf=game_df.merge(game_df,on='gameid',suffixes =['','_opponent'])
Yourdf[Yourdf.teamid != Yourdf.teamid_opponent]

   teamid  gameid  rebounds  teamid_opponent  rebounds_opponent
1       1       1        20                2                 35
2       2       1        35                1                 20
5       3       2        43                4                 15
6       4       2        15                3                 43

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

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