Pandas: groupby column A and make lists of tuples from other columns?
Make a new column for amount-time tuple atpair
df['atpair'] = list(zip(df.amount, df.time))
The data frame looks like
user time amount atpair
0 1 20 10.99 (10.99, 20)
1 1 10 4.99 (4.99, 10)
2 2 11 2.99 (2.99, 11)
3 2 18 1.99 (1.99, 18)
4 3 15 10.99 (10.99, 15)
Now perform groupby and apply list append to atpair
df = df.groupby('user')['atpair'].apply(lambda x : x.values.tolist())
The data frame looks like
user
1 [(10.99, 20), (4.99, 10)]
2 [(2.99, 11), (1.99, 18)]
3 [(10.99, 15)]
apply(list)
will consider the series index not the values .I think you are looking for
df.groupby('user')[['time', 'amount']].apply(lambda x: x.values.tolist())
user 1 [[23.0, 2.99], [50.0, 1.99]] 2 [[12.0, 1.99]]