I have a DataFrame:
df = pd.DataFrame({'ID':['a','b','d','d','a','b','c','b','d','a','b','a'],
'sec':[3,6,2,0,4,7,10,19,40,3,1,2]})
print(df)
ID sec
0 a 3
1 b 6
2 d 2
3 d 0
4 a 4
5 b 7
6 c 10
7 b 19
8 d 40
9 a 3
10 b 1
11 a 2
I want to calculate how many times a transition has occurred. Here in the ID column a->b is considered as a transition, similarly for b->d, d->d, d->a, b->c, c->b, b->a. I can do this using Counter like:
Counter(zip(df['ID'].to_list(),df['ID'].to_list()[1:]))
Counter({('a', 'b'): 3,
('b', 'd'): 2,
('d', 'd'): 1,
('d', 'a'): 2,
('b', 'c'): 1,
('c', 'b'): 1,
('b', 'a'): 1})
I also need to get min and max of the sec column of those transitions. Here for example a->b has occurred 3 times out of them min sec value is 1 and max sec value is 7. Also I want to get where this transition first occurred for a->b its 0. For the transition_index column I consider the first value of a transition, i.e. index of a and for calculating, min, max I take the second value of the transition, i.e. value at b.
Here is the final output I want to get:
df = pd.DataFrame({'ID_1':['a','b','d','d','b','c','b'],
'ID_2':['b','d','d','a','c','b','a'],
'sec_min':[1,2,0,3,10,19,2],
'sec_max':[7,40,0,4,10,19,2],
'transition_index':[0,1,2,3,5,6,10],
'count':[3,2,1,2,1,1,1]})
print(df)
ID_1 ID_2 sec_min sec_max transition_index count
0 a b 1 7 0 3
1 b d 2 40 1 2
2 d d 0 0 2 1
3 d a 3 4 3 2
4 b c 10 10 5 1
5 c b 19 19 6 1
6 b a 2 2 10 1
How can I achieve this in Python?
Also I have a huge amount of data, so I'm looking for the fastest way possible.