This is similar to Attach a calculated column to an existing dataframe, however, that solution doesn't work when grouping by more than one column in pandas v0.14.
For example:
$ df = pd.DataFrame([
[1, 1, 1],
[1, 2, 1],
[1, 2, 2],
[1, 3, 1],
[2, 1, 1]],
columns=['id', 'country', 'source'])
The following calculation works:
$ df.groupby(['id','country'])['source'].apply(lambda x: x.unique().tolist())
0 [1]
1 [1, 2]
2 [1, 2]
3 [1]
4 [1]
Name: source, dtype: object
But assigning the output to a new column result in an error:
df['source_list'] = df.groupby(['id','country'])['source'].apply(
lambda x: x.unique().tolist())
TypeError: incompatible index of inserted column with frame index
Merge grouped result with the initial DataFrame: