I have a dataframe as shown below:
id value
100 a
100 b
100 c
100 d
200 1
200 2
200 3
How do I roll up to the below format?
id value
100 ['a','b','c','d']
200 ['1','2','3']
You can groupby
on 'id' and then apply
list
to value
column and then call reset_index
:
In [13]:
df.groupby('id')['value'].apply(list).reset_index()
Out[13]:
id value
0 100 [a, b, c, d]
1 200 [1, 2, 3]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.