I have the following dataframe:
JavaScript
x
11
11
1
import pandas as pd
2
#Create DF
3
4
5
df = pd.DataFrame({
6
'Name': ['Jim','Jack','Jim','Jack','Jim','Jack','Mick','Mick'],
7
'Day':[1,1,2,2,3,3,4,4],
8
'Value':[10,20,30,40,50,60,70,80],
9
})
10
df
11
I would like to create a list for each unique Name
and append the Value
values to each list.
My expected output is:
JavaScript
1
4
1
Jim = [10,30,50]
2
Jack = [20,40,60]
3
Mick = [70,80]
4
Any ideas on the most efficient way to do this? thank you!
Advertisement
Answer
To aggregate as dictionary, you can use:
JavaScript
1
2
1
df.groupby('Name')['Value'].agg(list).to_dict()
2
Output:
JavaScript
1
4
1
{'Jack': [20, 40, 60],
2
'Jim': [10, 30, 50],
3
'Mick': [70, 80]}
4