python - Selecting rows in a dataframe where columns match and taking max -


so simplified version of full problem, more people if it's more generic.

so i'm using pandas dataframe that's arbitrarily big (large enough not write bunch of simple conditionals). looks this:

member    group    score 1         1        56 1         1        432 1         1        43  2         1        44 2         1        555 2         2        90 2         2        101 

and list goes on quite while. goal compare score of rows have both same member , group row, , take not max of rows how of max , store in new data frame. example, finished data frame like:

member    group    max    max 1         1        432    376 2         1        555    511 2         2        101    11 

i have no idea , have not found hint how compare rows without saying df['member'==1], there many different values member , group me this. thank in advance!

as far understand, want know max in each group , how max greater score in first row of group:

df1 = df.groupby(["group", "member"]).agg(["first", "max"]).reset_index() df1.columns = "member", "group", "first", "max" df1["max by"] = df1["max"] - df1["first"] #   member  group  first  max  max #0       1      1     56  432     376 #1       1      2     44  555     511 #2       2      2     90  101      11 

Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -