找到连胜次数最多的团队。

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

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&#39;m trying to find the team/s that have the most consecutive wins, but I don&#39;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[&#39;winner&#39;] = df[&#39;winner&#39;] + &#39; team&#39;

df_winners = df.melt([&#39;game&#39;, &#39;winner&#39;]).query(&#39;winner == variable&#39;)
print(df_winners)
print(df_winners.groupby(&#39;value&#39;)[&#39;game&#39;].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 = {
    &quot;game&quot;: [&quot;1201&quot;, &quot;1202&quot;, &quot;1203&quot;, &quot;1204&quot;, &quot;1205&quot;],
    &quot;home team&quot;: [&quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;D&quot;],
    &quot;away team&quot;: [&quot;A&quot;, &quot;B&quot;, &quot;C &quot;, &quot;D&quot;, &quot;A&quot;],
    &quot;winner&quot;: [&quot;home&quot;, &quot;away&quot;, &quot;home&quot;, &quot;away&quot;, &quot;away&quot;],
}
df = pd.DataFrame(df)

Then run,

df[&#39;winner&#39;] = df[&#39;winner&#39;] + &#39; team&#39;

df_winners = df.melt([&#39;game&#39;, &#39;winner&#39;]).query(&#39;winner == variable&#39;)
print(df_winners)
print(df_winners.groupby(&#39;value&#39;)[&#39;game&#39;].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

huangapple
  • 本文由 发表于 2023年6月5日 07:38:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402837.html
匿名

发表评论

匿名网友

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

确定