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

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

Find team/s that has the most consecutive wins

问题

这是你所需的翻译:

  1. 我正在尝试找到连胜场次最多的球队,但我不知道如何解决这个问题。
  2. 这是我的数据集:
  3. ```python
  4. df = {
  5. "game": ["1201", "1202", "1203", "1204", "1205"],
  6. "主队": ["B", "C", "D", "E", "D"],
  7. "客队": ["A", "B", "C", "D", "A"],
  8. "胜利者": ["主队", "客队", "主队", "客队", "客队"],
  9. }

我正在尝试获得类似以下的结果:

球队 胜利次数
B 2
D 2
  1. <details>
  2. <summary>英文:</summary>
  3. 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.
  4. 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"],
}

  1. I am trying to get something like this:
  2. | team | number of wins |
  3. | ---- | -------------- |
  4. | B | 2 |
  5. | D | 2 |
  6. </details>
  7. # 答案1
  8. **得分**: 2
  9. 尝试:
  10. ```python
  11. df['winner'] = df['winner'] + ' team'
  12. df_winners = df.melt(['game', 'winner']).query('winner == variable')
  13. print(df_winners)
  14. print(df_winners.groupby('value')['game'].count())

输出:

  1. game winner variable value
  2. 0 1201 home team home team B
  3. 6 1202 away team away team B
  4. 8 1204 away team away team D
  5. 9 1205 away team away team A
  6. value
  7. A 1
  8. B 2
  9. D 1
  10. Name: game, dtype: int64
  11. #### 更新后纠正输入数据框:
  12. df = {
  13. "game": ["1201", "1202", "1203", "1204", "1205"],
  14. "home team": ["B", "C", "D", "E", "D"],
  15. "away team": ["A", "B", "C ", "D", "A"],
  16. "winner": ["home", "away", "home", "away", "away"],
  17. }
  18. df = pd.DataFrame(df)
  19. 然后运行:
  20. ```python
  21. df['winner'] = df['winner'] + ' team'
  22. df_winners = df.melt(['game', 'winner']).query('winner == variable')
  23. print(df_winners)
  24. print(df_winners.groupby('value')['game'].count())

更新后输出:

  1. game winner variable value
  2. 0 1201 home team home team B
  3. 2 1203 home team home team D
  4. 6 1202 away team away team B
  5. 8 1204 away team away team D
  6. 9 1205 away team away team A
  7. value
  8. A 1
  9. B 2
  10. D 2
  11. Name: game, dtype: int64
英文:

Try:

  1. df[&#39;winner&#39;] = df[&#39;winner&#39;] + &#39; team&#39;
  2. df_winners = df.melt([&#39;game&#39;, &#39;winner&#39;]).query(&#39;winner == variable&#39;)
  3. print(df_winners)
  4. print(df_winners.groupby(&#39;value&#39;)[&#39;game&#39;].count())

Output:

  1. game winner variable value
  2. 0 1201 home team home team B
  3. 6 1202 away team away team B
  4. 8 1204 away team away team D
  5. 9 1205 away team away team A
  6. Value
  7. A 1
  8. B 2
  9. D 1
  10. Name: game, dtype: int64

Update after correcting input dataframe:

  1. df = {
  2. &quot;game&quot;: [&quot;1201&quot;, &quot;1202&quot;, &quot;1203&quot;, &quot;1204&quot;, &quot;1205&quot;],
  3. &quot;home team&quot;: [&quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;D&quot;],
  4. &quot;away team&quot;: [&quot;A&quot;, &quot;B&quot;, &quot;C &quot;, &quot;D&quot;, &quot;A&quot;],
  5. &quot;winner&quot;: [&quot;home&quot;, &quot;away&quot;, &quot;home&quot;, &quot;away&quot;, &quot;away&quot;],
  6. }
  7. df = pd.DataFrame(df)

Then run,

  1. df[&#39;winner&#39;] = df[&#39;winner&#39;] + &#39; team&#39;
  2. df_winners = df.melt([&#39;game&#39;, &#39;winner&#39;]).query(&#39;winner == variable&#39;)
  3. print(df_winners)
  4. print(df_winners.groupby(&#39;value&#39;)[&#39;game&#39;].count())

Updated Output:

  1. game winner variable value
  2. 0 1201 home team home team B
  3. 2 1203 home team home team D
  4. 6 1202 away team away team B
  5. 8 1204 away team away team D
  6. 9 1205 away team away team A
  7. value
  8. A 1
  9. B 2
  10. D 2
  11. 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:

确定