根据与熊猫的比赛持续时间计算胜率

时间:2018-02-09 14:26:31

标签: python pandas

我有两个看起来像这样的DataFrame:

   match_id hero_name    won
0         0    Rubick   True
1         9    Rubick  False
2        16    Rubick  False
3        28    Rubick   True
4        37    Rubick   True

   match_id  duration
0         0      2375
1         1      2582
2         2      2716
3         3      3085
4         4      1887

我试图按每个英雄的游戏长度计算胜率。到目前为止,我计算了每个5分钟的桶的百分比,但是我无法找到一种方法来绘制适合我计算的点的曲线。

以下是我尝试的内容:

matches = pd.merge(a, b, on='match_id')
matches['lost'] = ~matches['won']
# Ther's a single match with over 16000s, the others are all less than 6673
matches = matches[matches.duration < 7000]
# From 50s to 7000s with 5 minutes steps
ranges = np.arange(50, 7000, 300)

# Group by hero_name and duration range and count won and lost games
g = matches.groupby(['hero_name', pd.cut(matches.duration, ranges)]).sum()
# Win percentage
g['win_p'] = g['won'] / (g['won'] + g['lost'])
# Drop other columns and fills some NaN
g = g[['win_p']]
g = g.fillna(0)
g = g.reset_index()
g = g.groupby('hero_name')
n = g.ngroups

但我不知道如何绘制曲线。我已经尝试了kde(),但它只计算胜利而不是赢率,如果我在已计算的百分比上使用它,我得到它们的总和。如何根据每个英雄的持续时间插入曲线并绘制胜利率?

1 个答案:

答案 0 :(得分:0)

你需要在这里取消堆栈,更多信息请看here

g.win_p.unstack(level=0).plot( subplots=True)

enter image description here