I have a pandas dataframe
Type | Name |
---|---|
Cheese | Mozarella |
Cheese | Cheddar |
Cheese | Pepper Jack |
Bread | Sourdough |
Bread | Rye |
Pepper | Green |
I am trying to group by the Type column, collect the list of Names and split that into at most 2 columns. So the output would be:
Type | Name_primary | Name_alternative |
---|---|---|
Cheese | Mozarella | Cheddar |
Bread | Sourdough | Rye |
Pepper | Green |
- If a type has more than 2 Names, fill the first 2 for Name_primary and Name_alternative
- If a type has just one Name, the Name_alternative will be null
- Assumption : The first table has no duplicates and is already ordered in some way.
I got to the point of collecting list
df.groupby("Type").Name.apply(list).to_frame()
or
df.pivot_table(index="Type", values="Name", aggfunc=list)
I could do a lambda apply on a user function that checks the length, and gets (val1, val2) or (val1, None) and create Name_Primary and Name_Alternative columns. But is there a simpler way to do this ?