Python 如何根据Pandas中的多个条件从其他行获取值?
我有以下几点-Python 如何根据Pandas中的多个条件从其他行获取值?,python,pandas,Python,Pandas,我有以下几点- +--------+--------+--------------------+------------+--------------------+----------+----------+ | GameID | TeamID | Team | OpponentID | Opponent | Location | score | +--------+--------+-----------------
+--------+--------+--------------------+------------+--------------------+----------+----------+
| GameID | TeamID | Team | OpponentID | Opponent | Location | score |
+--------+--------+--------------------+------------+--------------------+----------+----------+
| 1 | 1 | Alabama | 2 | Jacksonville State | H | 1.098633 |
+--------+--------+--------------------+------------+--------------------+----------+----------+
| 1 | 2 | Jacksonville State | 1 | Alabama | V | 0.756562 |
+--------+--------+--------------------+------------+--------------------+----------+----------+
| 2 | 3 | UAB | 4 | Alcorn State | H | 1.270638 |
+--------+--------+--------------------+------------+--------------------+----------+----------+
| 2 | 4 | Alcorn State | 3 | UAB | V | 0.682791 |
+--------+--------+--------------------+------------+--------------------+----------+----------+
每行代表一个不同的GameID产生的两个团队结果中的一个。我的目标是有一个最终的df,看起来像这样
+--------+--------+--------------------+------------+--------------------+----------+----------+-----------------+
| GameID | TeamID | Team | OpponentID | Opponent | Location | score | opponents score |
+--------+--------+--------------------+------------+--------------------+----------+----------+-----------------+
| 1 | 1 | Alabama | 2 | Jacksonville State | H | 1.098633 | 0.756562 |
+--------+--------+--------------------+------------+--------------------+----------+----------+-----------------+
| 1 | 2 | Jacksonville State | 1 | Alabama | V | 0.756562 | 1.098633 |
+--------+--------+--------------------+------------+--------------------+----------+----------+-----------------+
| 2 | 3 | UAB | 4 | Alcorn State | H | 1.270638 | 0.682791 |
+--------+--------+--------------------+------------+--------------------+----------+----------+-----------------+
| 2 | 4 | Alcorn State | 3 | UAB | V | 0.682791 | 1.270638 |
+--------+--------+--------------------+------------+--------------------+----------+----------+-----------------+
我被困在如何查找与具有不同列名的条件匹配的值上。谢谢 您可以使用
merge()
方法:
resultdf=df.merge(df[['GameID','OpponentID','score']], left_on=['GameID','TeamID'], right_on=['GameID','OpponentID'], how='left')
现在使用drop()
方法:
result.drop(columns=['OpponentID_y'])
result=result.rename(columns={'OpponentID_x':'OpponentID','score_x':'score','score_y':'opponents score'})
最后使用rename()
方法:
result.drop(columns=['OpponentID_y'])
result=result.rename(columns={'OpponentID_x':'OpponentID','score_x':'score','score_y':'opponents score'})
现在,如果您打印结果
,您将很容易获得所需的输出。您可以使用以下代码实现所需的结果:
df_merge = df.merge(df, on=['GameID','TeamID'], how='outer', suffixes=(None, '_y'))
#print("Merged Dataset")
#display(df_merge)
df_merge.drop(['Team_y', 'OpponentID_y', 'Opponent_y', 'Location_y'], axis=1, inplace=True)
df_merge.rename( columns={'Score_y':'Opponent Score'}, inplace=True)
#print("Final Dataset")
#display(df_merge)
作品输出:
df.merge(df,左上=['GameID','TeamID',右上=['GameID','OpponentID','how'left')
并相应重命名。