英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论