I have a pandas dataframe with columns (among others) user_id and start_time. I want to efficiently and readably find all rows associated with each user's max start_time.
For example, if this were my data:
user_id start_time A B C
1 37 a b c
1 45 d e f
1 45 g h i
2 58 j k l
2 17 m n o
2 58 p q r
3 2 s t u
then I would expect to find
user_id start_time A B C
1 45 d e f
1 45 g h i
2 58 j k l
2 58 p q r
3 2 s t u
I've been coming up with solutions a bit like Conditional selection of data in a pandas DataFrame, but that finds the user_id with the latest start time, not the selection of the table for per-user max start_time's.
Of course, it's easy if I iterated the DataFrame by hand, but that is inefficient.
Thanks for any pointers.
For convenience to future readers, generate the dataframe thus:
columns = ['user_id', 'start_time', 'A', 'B', 'C']
LoL = [
[1, 37, 'a', 'b', 'c'],
[1, 45, 'd', 'e', 'f'],
[1, 45, 'g', 'h', 'i'],
[2, 58, 'j', 'k', 'l'],
[2, 17, 'm', 'n', 'o'],
[2, 58, 'p', 'q', 'r'],
[3, 2, 's', 't', 'u']]
pd.DataFrame = (LoL, columns=columns)