0

With a dataframe like this:

date           user    sales_blue     sales_red    calls_blue    calls_red
2019/06/01        A            30            50             3            4
2019/06/01        B            60            70             1            2
2019/06/02        A            25            35             4            6
2019/06/02        B            42            52             5            7

I'm trying to get to the following:

date           user    color    sales   calls
2019/06/01        A     blue       30       3
2019/06/01        A      red       50       4
2019/06/01        B     blue       60       1
2019/06/01        B      red       70       2
2019/06/02        A     blue       25       4
2019/06/02        A      red       35       6
2019/06/02        B     blue       42       5
2019/06/02        B      red       52       7

Pandas' documentation on melt is quite explicit when only one variable needs to be unpivoted, but I'm having a hard time understanding what to do for this case, where multiple variables are to be unpivoted (here, sales and calls).

Jivan
  • 21,522
  • 15
  • 80
  • 131
  • 1
    While that dupe only shows 2 columns, the approach generalizes to any number of `stubnames` – user3483203 Sep 16 '19 at 14:47
  • 1
    If you still need it, here is my (now deleted since its a dupe) answer: `pd.wide_to_long(df, stubnames=['sales', 'calls'], i=['date', 'user'], j='id', suffix='\w+', sep='_')` – user3483203 Sep 16 '19 at 14:48

0 Answers0