基于 Pandas 中的其他列的条件。

huangapple go评论52阅读模式
英文:

condition based on other column in pandas

问题

这是您要翻译的表格:

player team opp wl_g game_id match_id
Laz ZETA DRX lose 119512 184456
TENNN ZETA DRX lose 119512 184456
MaKo DRX ZETA win 119512 184456
Foxy9 DRX ZETA win 119512 184456
Laz ZETA DRX win 119513 184456
TENNN ZETA DRX win 119513 184456
MaKo DRX ZETA lose 119513 184456
Foxy9 DRX ZETA lose 119513 184456
Laz ZETA DRX lose 119514 184456
TENNN ZETA DRX lose 119514 184456
MaKo DRX ZETA win 119514 184456
Foxy9 DRX ZETA win 119514 184456

您需要在基于"wl_g"列的值相同的每个"match_id"上创建一个新列"wl_s",当"wl_g"列中的值为"win"时,"wl_s"列中的值应该相同。以下是您要的表格:

player team opp wl_g game_id match_id wl_s
Laz ZETA DRX lose 119512 184456 lose
TENNN ZETA DRX lose 119512 184456 lose
MaKo DRX ZETA win 119512 184456 win
Foxy9 DRX ZETA win 119512 184456 win
Laz ZETA DRX win 119513 184456 lose
TENNN ZETA DRX win 119513 184456 lose
MaKo DRX ZETA lose 119513 184456 win
Foxy9 DRX ZETA lose 119513 184456 win
Laz ZETA DRX win 119514 184456 lose
TENNN ZETA DRX win 119514 184456 lose
MaKo DRX ZETA lose 119514 184456 win
Foxy9 DRX ZETA lose 119514 184456 win
英文:

here's my table

player team opp wl_g game_id match_id
Laz ZETA DRX lose 119512 184456
TENNN ZETA DRX lose 119512 184456
MaKo DRX ZETA win 119512 184456
Foxy9 DRX ZETA win 119512 184456
Laz ZETA DRX win 119513 184456
TENNN ZETA DRX win 119513 184456
MaKo DRX ZETA lose 119513 184456
Foxy9 DRX ZETA lose 119513 184456
Laz ZETA DRX lose 119514 184456
TENNN ZETA DRX lose 119514 184456
MaKo DRX ZETA win 119514 184456
Foxy9 DRX ZETA win 119514 184456

and i need to make a column based on wl_g column with same each value on match_id, im expecting to get value for new colum wl_s(a.k.a win lose for each match_id) when win on wl_g column = 2, is equal value win on wl_s column . Here's the table that i want

player team opp wl_g game_id match_id wl_s
Laz ZETA DRX lose 119512 184456 lose
TENNN ZETA DRX lose 119512 184456 lose
MaKo DRX ZETA win 119512 184456 win
Foxy9 DRX ZETA win 119512 184456 win
Laz ZETA DRX win 119513 184456 lose
TENNN ZETA DRX win 119513 184456 lose
MaKo DRX ZETA lose 119513 184456 win
Foxy9 DRX ZETA lose 119513 184456 win
Laz ZETA DRX win 119514 184456 lose
TENNN ZETA DRX win 119514 184456 lose
MaKo DRX ZETA lose 119514 184456 win
Foxy9 DRX ZETA lose 119514 184456 win

答案1

得分: 1

将"wl_g"和"match_id"列作为基础,添加"wl_s"列:

data = {
    "player": ["Laz", "TENNN", "MaKo", "Foxy9", "Laz", "TENNN", "MaKo", "Foxy9", "Laz", "TENNN", "MaKo", "Foxy9"],
    "team": ["ZETA", "ZETA", "DRX", "DRX", "ZETA", "ZETA", "DRX", "DRX", "ZETA", "ZETA", "DRX", "DRX"],
    "opp": ["DRX", "DRX", "ZETA", "ZETA", "DRX", "DRX", "ZETA", "ZETA", "DRX", "DRX", "ZETA", "ZETA"],
    "wl_g": ["lose", "lose", "win", "win", "win", "win", "lose", "lose", "lose", "lose", "win", "win"],
    "game_id": [119512, 119512, 119512, 119512, 119513, 119513, 119513, 119513, 119514, 119514, 119514, 119514],
    "match_id": [184456, 184456, 184456, 184456, 184456, 184456, 184456, 184456, 184456, 184456, 184456, 184456]
}

df = pd.DataFrame(data)

df["wl_s"] = df.groupby("match_id")["wl_g"].transform(lambda x: "win" if (x == "win").any() else "lose")

# 打印结果DataFrame
print(df)
英文:

Add the "wl_s" column based on "wl_g" and "match_id"

data = {
    "player": ["Laz", "TENNN", "MaKo", "Foxy9", "Laz", "TENNN", "MaKo", "Foxy9", "Laz", "TENNN", "MaKo", "Foxy9"],
    "team": ["ZETA", "ZETA", "DRX", "DRX", "ZETA", "ZETA", "DRX", "DRX", "ZETA", "ZETA", "DRX", "DRX"],
    "opp": ["DRX", "DRX", "ZETA", "ZETA", "DRX", "DRX", "ZETA", "ZETA", "DRX", "DRX", "ZETA", "ZETA"],
    "wl_g": ["lose", "lose", "win", "win", "win", "win", "lose", "lose", "lose", "lose", "win", "win"],
    "game_id": [119512, 119512, 119512, 119512, 119513, 119513, 119513, 119513, 119514, 119514, 119514, 119514],
    "match_id": [184456, 184456, 184456, 184456, 184456, 184456, 184456, 184456, 184456, 184456, 184456, 184456]
}

df = pd.DataFrame(data)


df["wl_s"] = df.groupby("match_id")["wl_g"].transform(lambda x: "win" if (x == "win").any() else "lose")

# Print the resulting DataFrame
print(df)

huangapple
  • 本文由 发表于 2023年5月23日 00:39:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定