I have a dataframe like this:
>>> df = pd.DataFrame([['bar',0],['restaurant',0],
... ['bar',0],
... ['movie',1],['bar',1],['restaurant',1],
... ['bar',2],['restaurant',2],['movie',2]]
... ,columns=['place','ID'])
>>> df
place ID
0 bar 0
1 restaurant 0
2 bar 0
3 movie 1
4 bar 1
5 restaurant 1
6 bar 2
7 restaurant 2
8 movie 2
I would like to group by the ID and get the unique combinations of places, in order, with repeats allowed. If I do this:
>>> df.groupby('ID')['place'].unique().apply(lambda x: tuple(sorted(x))).reset_index()
ID place
0 0 (bar, restaurant)
1 1 (bar, movie, restaurant)
2 2 (bar, movie, restaurant)
It is almost correct, but I want repeats allowed:
place ID
0 (bar, bar, restaurant) 0
1 (bar, movie, restaurant) 1
2 (bar, movie, restaurant) 2