Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python :Select the rows for the most recent entry from multiple users [closed]

I have a dataframe df with 3 columns :

df=pd.DataFrame({
    'User':['A','A','B','A','C','B','C'],
    'Values':['x','y','z','p','q','r','s'],
    'Date':[14,11,14,12,13,10,14]
})

I want to create a new dataframe that will contain the rows corresponding to highest values in the 'Date' columns for each user. For example for the above dataframe I want the desired dataframe to be as follows ( its a jpeg image):

image

Can anyone help me with this problem?

like image 280
RemyM Avatar asked May 06 '26 21:05

RemyM


1 Answers

This answer assumes that there is different maximum values per user in Values column:

In [10]: def get_max(group):
    ...:     return group[group.Date == group.Date.max()]
    ...: 

In [12]: df.groupby('User').apply(get_max).reset_index(drop=True)
Out[12]: 
   Date User Values
0    14    A      x
1    14    B      z
2    14    C      s
like image 152
iDrwish Avatar answered May 08 '26 10:05

iDrwish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!