简体   繁体   中英

Get specific column value of last row of a unique user in pandas

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.

You can use groupby_last :

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

Output:

    user friend
0    One    Two
1  Three   Two.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM