【问题标题】:Calculating average of specific rows计算特定行的平均值
【发布时间】:2017-03-22 21:03:15
【问题描述】:

我的代码是:

df=pandas.read_csv("data.csv", names=["Date","Team1","Team2","Map","Event"])
df

输出是:

Date, Team1, Team2, Team1 Score, Team2 Score, Map, Event
17/3/17, Misfits, Cloud9, 16, 4,overpass, Pro League
17/3/17, TyLoo, Misfits, 16, 4, cache, Pro League
17/3/17, Liquid, Misfits, 8, 16, cbble, Proleague
17/3/17, Misfits, Blight, 16, 7, overpass, Proleague
17/3/17, Selfless, Misfits, 12, 16, inferno, Proleague

我的目标是计算团队“Misfits”的平均得分,即 (16+4+16+16+16)/5=13.6

我知道我可以通过 df["Column Name"].mean() 计算列平均值,但问题是团队“Misfits”出现在“Team1”和“Team2”列中。

【问题讨论】:

    标签: python pandas dataframe jupyter-notebook


    【解决方案1】:
    df[['Team1 Score', 'Team2 Score']].where(
        df[['Team1', 'Team2']].values == 'Misfits').stack().mean()
    
    13.6
    

    【讨论】:

    • 谢谢,这正是我的意思。 :)
    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 2021-05-16
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    • 1970-01-01
    • 2016-10-13
    相关资源
    最近更新 更多