I’ve following toy-dataframe:
| id| date
--------------
0 | a | d1
1 | b | d1
2 | a | d2
3 | c | d2
4 | b | d3
5 | a | d3
import pandas as pd
df = pd.DataFrame({'id': ['a', 'b', 'a', 'c', 'b', 'a'], 'date': ['d1', 'd1', 'd2', 'd2', 'd3', 'd3']})
I want to obtaining ‘linking dicitionary’, like this: d = {0: 2, 2: 5, 1: 4}
,
where (numbers are just row index)
0:2
means linka
fromd1
toa
fromd2
,2:5
means linka
fromd2
toa
fromd3
,1:4
means linkb
fromd1
tob
fromd3
Is there some simple and clean way to get it?
>Solution :
You can use groupby
and reduce
:
from functools import reduce
d = df.groupby('id').apply(lambda x: dict(zip(x.index, x.index[1:])))
d = reduce(lambda d1, d2: {**d1, **d2}, d) # or reduce(lambda d1, d2: d1 | d2, d)
print(d)
# Output
{0: 2, 2: 5, 1: 4}