nielsen
nielsen

Reputation: 383

How to create an ID starting from 1, which after sorting starts over each time another (string, time) variable increases by 1 (minute)?

As you can see here Variables, I have four variables. Each ID represents a section of a road, which has a speed recorded at every timestamp. However, this section has 16 subsections with each their own speed. These 16 speeds have been created as one column with 16 rows instead of 16 columns, but the IDs just go from 1-16 over and over making them non-unique.

What I need is to create a unique ID starting from one, that means that for each time stamp I have (# of IDs * 16 subsections) IDs. In other words, if it is sorted by time stamp, then ID then subsection, I need it to create an ID from 1 that starts over from 1 every time it increases by one minute.

I hope some of you can help me with this. It would be greatly appreciated.

Upvotes: 1

Views: 414

Answers (1)

katardin
katardin

Reputation: 606

Got it, here's an example - you could alter the new_id line if you want a different format.

def make_id(row):
    new_id = (row['ID']-1)*16 + row['Segment']
    return new_id

df['UniqID'] = df.apply(make_id, axis = 1)

The output would be 1 for Section 1, Subsection 1, 16 for Section 1, Subsection 16.

Upvotes: 1

Related Questions