1
import pandas as pd
df1 = pd.DataFrame( {"name" : ["name1", "name1", "name2", "name3", "name3" , "name3"], "item" : ["item1", "item2", "item3", "item2", "item3", "item1"] } )
df2 = df1.groupby('name')['item']

In this groupby object I have all the items for each name. I can visualise this through:

print(df2.apply(list))

and get:

name
name1           [item1, item2]
name2                  [item3]
name3    [item2, item3, item1]

what I, however, would like to achieve is something like this:

name     itemA    itemB    itemC
name1    item1    item2    NaN
name2    item3    NaN      NaN
name3    item2    item3    item1

Any help is much appreciated.

2 Answers 2

2

Adding apply pd.Series at the end of your df2

df2.apply(list).apply(pd.Series)
Out[300]: 
           0      1      2
name                      
name1  item1  item2    NaN
name2  item3    NaN    NaN
name3  item2  item3  item1
Sign up to request clarification or add additional context in comments.

3 Comments

Excellent. That's perfect. Thanks a lot.
@Christian yw :-)
To me it seems like the object is not as asked for. The example has a flat header structure, while the answers result has the multi index structure. Can one flatten it out well?
-1

Added column name in Wen's answer

>>> df3 = df2.apply(list).apply(pd.Series)
>>> df3.columns = ['itemA', 'itemB', 'itemC']
# df3 = df3.rename(columns={0: 'itemA', 1: 'itemB', 2: 'itemC'})
>>> df3
       itemA  itemB  itemC
name                      
name1  item1  item2    NaN
name2  item3    NaN    NaN
name3  item2  item3  item1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.