英文:
Find team/s that has the most consecutive wins
问题
这是你所需的翻译:
我正在尝试找到连胜场次最多的球队,但我不知道如何解决这个问题。
这是我的数据集:
```python
df = {
"game": ["1201", "1202", "1203", "1204", "1205"],
"主队": ["B", "C", "D", "E", "D"],
"客队": ["A", "B", "C", "D", "A"],
"胜利者": ["主队", "客队", "主队", "客队", "客队"],
}
我正在尝试获得类似以下的结果:
球队 | 胜利次数 |
---|---|
B | 2 |
D | 2 |
<details>
<summary>英文:</summary>
I'm trying to find the team/s that have the most consecutive wins, but I don't know how to approach the question.
This is my dataset:
df = {
"game": ["1201", "1202", "1203", "1204", "1205"],
"home team": ["B", "C", "D", "E", "D"],
"away team": ["A", "B", "C ", "D", "A"],
"winner": ["home", "away", "home ", "away", "away"],
}
I am trying to get something like this:
| team | number of wins |
| ---- | -------------- |
| B | 2 |
| D | 2 |
</details>
# 答案1
**得分**: 2
尝试:
```python
df['winner'] = df['winner'] + ' team'
df_winners = df.melt(['game', 'winner']).query('winner == variable')
print(df_winners)
print(df_winners.groupby('value')['game'].count())
输出:
game winner variable value
0 1201 home team home team B
6 1202 away team away team B
8 1204 away team away team D
9 1205 away team away team A
value
A 1
B 2
D 1
Name: game, dtype: int64
#### 更新后纠正输入数据框:
df = {
"game": ["1201", "1202", "1203", "1204", "1205"],
"home team": ["B", "C", "D", "E", "D"],
"away team": ["A", "B", "C ", "D", "A"],
"winner": ["home", "away", "home", "away", "away"],
}
df = pd.DataFrame(df)
然后运行:
```python
df['winner'] = df['winner'] + ' team'
df_winners = df.melt(['game', 'winner']).query('winner == variable')
print(df_winners)
print(df_winners.groupby('value')['game'].count())
更新后输出:
game winner variable value
0 1201 home team home team B
2 1203 home team home team D
6 1202 away team away team B
8 1204 away team away team D
9 1205 away team away team A
value
A 1
B 2
D 2
Name: game, dtype: int64
英文:
Try:
df['winner'] = df['winner'] + ' team'
df_winners = df.melt(['game', 'winner']).query('winner == variable')
print(df_winners)
print(df_winners.groupby('value')['game'].count())
Output:
game winner variable value
0 1201 home team home team B
6 1202 away team away team B
8 1204 away team away team D
9 1205 away team away team A
Value
A 1
B 2
D 1
Name: game, dtype: int64
Update after correcting input dataframe:
df = {
"game": ["1201", "1202", "1203", "1204", "1205"],
"home team": ["B", "C", "D", "E", "D"],
"away team": ["A", "B", "C ", "D", "A"],
"winner": ["home", "away", "home", "away", "away"],
}
df = pd.DataFrame(df)
Then run,
df['winner'] = df['winner'] + ' team'
df_winners = df.melt(['game', 'winner']).query('winner == variable')
print(df_winners)
print(df_winners.groupby('value')['game'].count())
Updated Output:
game winner variable value
0 1201 home team home team B
2 1203 home team home team D
6 1202 away team away team B
8 1204 away team away team D
9 1205 away team away team A
value
A 1
B 2
D 2
Name: game, dtype: int64
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论