-1

for example, my data frame is:

ID time number
a 14:03:01 11
b 14:03:02 7
b 14:03:15 2
c 14:03:09 5
a 14:03:02 9
d 14:03:17 1
a 14:03:35 15
c 14:03:11 8

I sort this data frame by time and for each ID I want to get the value of the number column for the earliest time. I know the solution is SQL but now, I get confused to do it for pandas.

ID number
a 11
b 7
c 5
d 1

How can I do these using pandas? (I don't want to use "for loop" .)

WhoIsKi
  • 117
  • 1
  • 14
  • Does this answer your question? [Pandas dataframe get first row of each group](https://stackoverflow.com/questions/20067636/pandas-dataframe-get-first-row-of-each-group) – Nimantha Oct 25 '21 at 16:00

1 Answers1

3

try via sort_values() method ,drop_duplicates() method and drop() method:

out=df.sort_values('time').drop_duplicates(subset=['ID']).drop('time',1)

OR

via groupby() and first():

out=df.groupby('ID',as_index=False)['number'].first()
Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41