Text can be used as input to pandas dataframes to make easily reproducible examples for testing solutions.1
import pandas as pd
from io import StringIO
txt= """
ID,datetime,value
AB-CL-34,07/10/2022 10:00:00,5
AB-CL-34,07/10/2022 11:15:10,7
AB-CL-34,09/10/2022 15:30:30,13
BX-RT-55,06/10/2022 11:30:22,0
BX-RT-55,10/10/2022 22:44:11,1
BX-RT-55,10/10/2022 23:30:22,6
"""
df = pd.read_csv(StringIO(txt), parse_dates=[1], dayfirst=True)
How do I index df
such that there is an integer index according to each ID, keeping the ID variable?
imagined output
ID datetime value
0 AB-CL-34 07/10/2022 10:00:00 5
0 AB-CL-34 07/10/2022 11:15:10 7
0 AB-CL-34 09/10/2022 15:30:30 13
1 BX-RT-55 06/10/2022 11:30:22 0
1 BX-RT-55 10/10/2022 22:44:11 1
1 BX-RT-55 10/10/2022 23:30:22 6
edit ID values are now chr/str, not int, as they should have been (apologies to Marcus_CH.