How to get dictionary of df indices that links the same ids on different days?

Question:

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 link a from d1 to a from d2,
  • 2:5 means link a from d2 to a from d3,
  • 1:4 means link b from d1 to b from d3

Is there some simple and clean way to get it?

Asked By: Quant Christo

||

Answers:

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}
Answered By: Corralien

Use dictionary comprehension:

d = {k: v for _, x in df.groupby('id') for k, v in zip(x.index, x.index[1:])}
print (d)
{0: 2, 2: 5, 1: 4}
Answered By: jezrael
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.