1

I have a Pandas dataframe like so

movie, week, sales
-----, ----, ------
1,    1,     100
...
1,    52,    200 
2,    1,     500,
...

What I actually want it to look like is

movie, week_1, ... week_52, 
1,     1,          200
2,     1,          500

So what's effectively happened is there is now one row per movie and one column per week and the value of df[movie, week] equals the sales of that movie on that week.

I've tried df.transpose() and df.pivot(columns=['week', 'sales']) but neither is accomplishing the intended effect.

What the Pandas way to do this?

George
  • 1,196
  • 1
  • 2
  • 10
jbuddy_13
  • 902
  • 2
  • 12
  • 34
  • 3
    Try: `df.pivot(*df).add_prefix('week_').reset_index()` See [this answer](https://stackoverflow.com/a/73060100/19123103) for more info. – cottontail Jan 23 '23 at 21:51

0 Answers0