1

Given dataframe in this format:

Name      Attribute      Answer
Joe       One            Yes
Joe       Two            No
Joe       Two            More info
Mary      One            Left undone
Mary      Three          No response
Mary      One            Too late

I have tried versions of pivot, pivotable, unstack etc to 'unmelt' this data from long to wide format. The result I am seeking is this:

Name       One                        Two                Three
Joe        Yes                        No, More info      Null
Mary       Left undone, Too late      Null               No response

Essentially, I need to make all the unique values in the Attribute column into column headers and then the value in the Attribute column for each unique named person be the value in the Answer column

I am certain there is some reshaping magic I have not pieced together sufficiently, but the typical approaches and reading more than 20 'reshape data long to wide' questions on SO were not on point.

I posed this question two hours or so ago and someone closed it claiming it was already answered. Welp, went through each of the supposed answers to this and none of them succeeded. So, my question has not already been answered. FYI.

John Taylor
  • 655
  • 6
  • 19

1 Answers1

2

Try:

df.groupby(['Name','Attribute'])['Answer'].agg(lambda x: ', '.join(x)).unstack().reset_index()

Output:

Attribute  Name                    One        Three            Two
0           Joe                    Yes          NaN  No, More info
1          Mary  Left undone, Too late  No response            NaN
Scott Boston
  • 147,308
  • 15
  • 139
  • 187
  • Thanks @Scott Boston. This groupy example worked perfectly. Even had a request to insert a ';' instead of a comma and that worked well too. Thanks again! – John Taylor Apr 10 '21 at 12:51