I have the following DataFrame:
import pandas as pd
data = {'id': ['A', 'A', 'A', 'B', 'B', 'B', 'B'],
'location':['Milan', 'Paris', 'New York', 'Rome', 'Los Angeles', 'Berlin', 'Madrid'],
'year': [2003,2004,2005, 2003, 2004, 2004, 2005]}
data = pd.DataFrame(data)
For each groupby('id'), I want the combinations among the city i at year t and all the cities at year t-1, t-2, ..., t-n.
The desired output:
data = {'id': ['A', 'A', 'A', 'A',
'B', 'B', 'B', 'B', 'B', 'B'],
'location':['Milan', 'Paris', 'New York', 'New York',
'Rome', 'Los Angeles', 'Berlin', 'Madrid','Madrid', 'Madrid'],
'year': [2003, 2004, 2005, 2005,
2003, 2004, 2004, 2005, 2005, 2005],
'comb': ['NaN', 'Milan', 'Milan','Paris',
'NaN', 'Rome', 'Rome', 'Rome','Los Angeles', 'Berlin']}
data = pd.DataFrame(data)