我有这个示例数据集: HOME_TEAM AWAY_TEAM TIMES0 Argentina Uruguay 31 Uruguay Argentina 5我想了解这支球队在其中所经历的时间。 Matches TIMES3 Argentina-Uruguay 8我一直在尝试以下代码:af = af.merge( af, left_on=["home_team", "away_team"], right_on=["away_team", "home_team"], how='left')输出是: home_team_x away_team_y values_x home_team_y away_team_y values_y0 Argentina Uruguay 96 Uruguay Argentina 80.01 Uruguay Argentina 80 Argentina Uruguay 96.0有没有一种方法可以以Python方式实现并避免重复?
1 回答
元芳怎么了
TA贡献1798条经验 获得超7个赞
一种使用方法pandas.Series.groupby.sum:
new_df = df["TIMES"].groupby(df.filter(like="TEAM").apply(frozenset, 1)).sum()
new_df.index = ["-".join([t1, t2]) for t1, t2 in new_df.index]
print(new_df.rename_axis("Matches").reset_index())
输出:
Matches TIMES
0 Argentina-Uruguay 8
添加回答
举报
0/150
提交
取消