pfi.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:PyData-PandasFromTheInside 作者: stevesimmons 项目源码 文件源码
def calc_ladder(scores_df, year=2016):
    '''
    DataFrame with championship ladder with round-robin games for the given year.
    Wins, draws and losses are worth 4, 2 and 0 points respectively.
    '''
    # Select a subset of the rows
    # df.loc[] matches dates as strings like '20160506' or '2016'.
    # Note here rounds are simple strings so sort with R1 < R10 < R2 < .. < R9
    #      (we could change this with a CategoricalIndex)
    # Note also that pandas 0.18.0 has a bug with .loc on MultiIndexes
    #      if dates are the first level. It works as expected if we
    #      move the dates to the end before slicing
    scores2 = scores_df.reorder_levels([1, 2, 3, 0]).sort_index()
    x = scores2.loc(axis=0)[:, 'R1':'R9', :, str(year):str(year)]
    # Don't need to put levels back in order as we are about to drop 3 of them
    # x = x.reorder_levels([3, 0, 1, 2]).sort_index()

    # Just keep Team. This does a copy too, avoiding SettingWithCopy warning
    y = x.reset_index(['Date', 'Venue', 'Round'], drop=True)

    # Add cols with 0/1 for number of games played, won, drawn and lost
    y['P'] = 1
    y['W'] = (y['F'] > y['A']).astype(int)
    y['D'] = 0
    y.loc[y['F'] == y['A'], 'D'] = 1
    y.eval('L = 1*(A>F)', inplace=True)
    print(y)

    # Subtotal by team and then sort by Points/Percentage
    t = y.groupby(level='Team').sum()
    t['PCT'] = 100.0 * t.F / t.A
    t['PTS'] = 4 * t['W'] + 2 * t['D']
    ladder = t.sort_values(['PTS', 'PCT'], ascending=False)

    # Add ladder position (note: assumes no ties!)
    ladder['Pos'] = pd.RangeIndex(1, len(ladder) + 1)
    print(ladder)

    return ladder
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号