I have data in a frame that looks like:
Region Date Drip Coffee Espresso Latte Other
Central 1 5 1 2 3
East 1 3 3 1 4
North 1 5 1 3 2
Central 2 2 7 2 0
East 2 10 3 2 1
North 2 6 9 4 2
.
.
.
I want to pivot Drip Coffee, Espresso, Latte, and Other so that it lines up like so with repetitions on Date and Region like:
Region Date Type Value
Central 1 Drip Coffee 5
East 1 Drip Coffee 3
North 1 Drip Coffee 5
Central 1 Espresso 1
East 1 Espresso 3
North 1 Espresso 1
.
.
.
Central 2 Drip Coffee 2
East 2 Drip Coffee 10
North 2 Drip Coffee 6
.
.
I've tried a few methods like:
df_new = df_old.pivot(index='Date',columns=['Drip Coffee', 'Espresso', 'Latte', 'Other']).stack(0).rename_axis(['Date','Type']).reset_index()
But this gives me ValueError: all arrays must be same length
I understand that I'm missing a new column here in my test for Value
but it's because I don't understand how to pivot such a series of values like this.
I'd like to see if there's a possible fix because this problem seems to be quite unique; and I cannot find such a multiple repetition solution out there.