Pandas: Syntax in Groupby

Viewed 336

I have the following dataset:

id window Rank    member
1    2      2        0
1    3      2        0 
2    3      1        0 
2    2      1        0

I want to make member to be equal to Rank when window==3. To do that, I have the following command:

df["member"]= df[df['window']==3]['Rank'][0]

However, I want to do that in a groupby statement grouping on id. The command below returns an error. It is probably a simple thing that I am missing here, but I cannot get around it how to use groupby in the above command.Any help is greatly appreciated.

df["member"]= df.groupby("id")[df[df['window']==3]['Rank'][0]]
2 Answers
Related