Juho M Juho M -4 years ago 115
Python Question

Calculating average of specific rows

My code is:

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


And the output is:

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


My goal is to calculate team "Misfits" score average, which is (16+4+16+16+16)/5=13.6

I know that I can calculate column average by df["Column Name"].mean(), but the problem is that team "Misfits" appears in "Team1" and "Team2" columns.

Answer Source
df[['Team1 Score', 'Team2 Score']].where(
    df[['Team1', 'Team2']].values == 'Misfits').stack().mean()

13.6
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download