Jian Jian - 25 days ago 9
Python Question

Pandas: how to change a Series into nested list by index

My Series look like this:


group
SAM 1
SAM 1
PEG-PROD 1
TAG 1
PEG-PROD 1
SAM 1
PEG-MAR 1
SIG-A 1
SAM 1
PEG-MAR 1

I wanna change it into a nested list based on group like this:

SAM 1,1,1,1
PEG-PROD 1,1
PEG_MAR 1,1
TAG 1
SIG-A 1

Can anyone help? Thanks!!!

Answer

IIUC:

s.groupby(s.index).apply(list)

where s is your Series