0

Hoping I can get help with a tricky transposition. I have a pandas data frame with 4 columns:

  • Customer name
  • Customer ID
  • Time period
  • Revenue

In the current structure, a customer name / ID will show up in multiple rows because they have revenue occurring in different periods.

I'd like to do a partial transposition of the data frame to where the new data frame as the following columns:

  • Customer name
  • Customer ID
  • Period 1 Revenue
  • Period 2 Revenue
  • Period 3 Revenue
  • ...
  • Period N Revenue

In the new data structure, each customer name / ID would only show up in a single row, with all their relevant revenue data in the same row.

Example of current df structure

Example of the df I'd like to transform it into

I've tried / thought through a few ideas (e.g., using .transpose(), creating new columns using np.where and then doing some sort of sum / groupby to collapse the rows), but have struggled to find a simple solution. Any help appreciated!

0 Answers0