Home > Back-end >  Get specific column value of last row of a unique user in pandas
Get specific column value of last row of a unique user in pandas

Time:01-26

I have a table like this

user friend food
One Two apple
Three Four hello
Three Two. hey.

My desired output is this

user friend
One Two
Three Two.

Thanks.

CodePudding user response:

You can use groupby_last:

out = df.groupby('user')['friend'].last().reset_index()

Output:

    user friend
0    One    Two
1  Three   Two.
  •  Tags:  
  • Related