Skip to content
Advertisement

Data-frame columns into list of lists using Groupby

A data-frame and I want to transform it.

data = {'Number': [536, 578, 36, 468, 86], 
'Content' : ["Banana","Apple","Orange","Mango","Grape"],
'Quantity': [1, 2, 5, 2, 6],
'Origin': ["TX","TX","OP","OP","OP"]}
df = pd.DataFrame(data)

enter image description here

The ideal result is something like:

OP    [[36,5,Orange], [86,6,Grape], [468,2,Mango]]
TX             [[536,1,Banana], [578,2,Apple]]

I tried:

df.groupby(['Origin', 'Number', 'Quantity'])['Content'].apply(list))

also:

df.groupby(['Origin', 'Number', 'Quantity'])'Content'].apply(list)).groupby(level=0).apply(list)

But not getting nearer.

What’s the right way?

Advertisement

Answer

You can do:

df.groupby('Origin').apply(lambda x: x.drop('Origin', axis=1).values.tolist())

Output:

Origin
OP    [[36, Orange, 5], [468, Mango, 2], [86, Grape,...
TX                  [[536, Banana, 1], [578, Apple, 2]]
dtype: object

If you want the list in the correct order, you would need to re-order your columns. For example:

(df[['Number', 'Quantity', 'Content']]
    .groupby(df['Origin'])
    .apply(lambda x: x.values.tolist())
)

And you get:

Origin
OP    [[36, 5, Orange], [468, 2, Mango], [86, 6, Gra...
TX                  [[536, 1, Banana], [578, 2, Apple]]
dtype: object
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement