I have the following dataset:
value timestamp
0 Fire 2017-10-03 14:33:52
1 Water 2017-10-04 14:33:48
2 Fire 2017-10-04 14:33:45
3 Fire 2017-10-05 14:33:30
4 Water 2017-10-03 14:33:40
5 Water 2017-10-05 14:32:13
6 Water 2017-10-04 14:32:01
7 Fire 2017-10-03 14:31:55
I want to group this set by timestamp
per day and then only select the earliest row per day. For the above example the following should be the result:
value timestamp
1 Water 2017-10-05 14:32:13
2 Water 2017-10-04 14:32:01
3 Fire 2017-10-03 14:31:55
For example, for the day 2017-10-03
there are 3 entries but I only want the earliest on that day.
If you have unique index, you can use idxmin
on timestamp
to find out the indices of the minimum timestamp and extract them with loc
:
df.timestamp = pd.to_datetime(df.timestamp)
df.loc[df.groupby(df.timestamp.dt.date, as_index=False).timestamp.idxmin()]
# value timestamp
#7 Fire 2017-10-03 14:31:55
#6 Water 2017-10-04 14:32:01
#5 Water 2017-10-05 14:32:13
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With