I am working on getting the index of the first and last occurrence of IDs in a data frame. But if the ID only appears once, then the last occurrence will be the same as the first one.
For example, a data like this:
ID  Date
A   1/1/2015
A   1/5/2016
A   1/3/2017
B   1/3/2017
C   1/5/2016
C   1/7/2016
and the output will be
ID  Index   Date
A   0   1/1/2015
A   2   1/3/2017
B   3   1/3/2017
B   3   1/3/2017
C   4   1/5/2016
C   5   1/5/2016
Note: I don't really need the index, it is just for making the question clearer.
I have tried using data.groupby('ID', as_index=False).nth([0,-1]) but in the example above, this will only output B once.
Thanks in advance