0

I have a pandas DataFrame like this:

Id  Name        Result
1   rating      good
1   feedback    ok
2   rating      average
2   number      5

I want to pivot Name column value as new column name, and corresponding Result column as cell value after groupby Id column, for a given Id, if corresponding Name is absent, the cell value will be NaN. For each given Id, there is no duplicate in Name column. The result will be:

Id  rating     feedback       number
1     good       ok           NaN
2     average    NaN          5

If there are >10000 rows, how to do this transformation efficiently?

Arthur
  • 91
  • 3
  • use `pd.pivot_table` to reshape, instead of a groupby. For the dataframe shared, a pivot is fine `df.pivot('Id', 'Name', 'Result')` – sammywemmy Oct 25 '21 at 00:15

0 Answers0