I am stuck on what is probably a very simple problem. My data looks something like this:
| id_not_unique | datetime | seconds |
|---|---|---|
| 111111111 | 2020-08-26 15:44:58 | 122 |
| 111111111 | 2020-08-28 15:33:45 | 34 |
| 222222222 | 2020-07-12 11:21:09 | 26 |
| 222222222 | 2019-04-21 14:22:42 | 57 |
So I want to group by id_not_unique and then add a new column called time that is just finding the minimum datetime in the group and then return the corresponding seconds. So the result would look something like:
| id_not_unique | time |
|---|---|
| 111111111 | 122 |
| 222222222 | 57 |
I have tried this:
def wait_first_call(df):
min_time = min(df['datetime'])
idx = df.index[df.datetime == min_time]
first_time = df['seconds'].iloc[idx]
return first_time
then
df.groupby(['id_not_unique']).apply(wait_first_call)
But I keep getting an "IndexError: positional indexers are out-of-bounds". So I am not understanding why I am getting this error -- I thought the apply function took each group as a dataframe and applies the function to this group?
Any suggestions/help would be greatly appreciated.