I have a dataframe which has horizontal identifiers (yes and no) and values, and I want to melt it into vertical values into each yes. Here is a snippet of my dataframe:
option Region Store Name option1 option2 option3 option4 profit
0 Region 1 Store 1 Y Y N N 48.1575
1 Region 1 Store 2 N Y N Y 74.7667
2 Region 1 Store 3 N Y N Y 102.35
3 Region 2 Store 4 N Y N Y 114.59
4 Region 2 Store 5 N Y N Y 99.705
5 Region 2 Store 6 N Y N Y 105.07
The answer is need to get is:
option Region Store Name options profit
0 Region 1 Store 1 option1 48.1575
1 Region 1 Store 1 option2 48.1575
2 Region 1 Store 2 option2 74.7667
3 Region 1 Store 2 option4 74.7667
Essentially, I need to unstack the customer options tables, assign the same profit to everything with a yes, and drop everything with a no.
So far, the function I used is:
e1 = pd.melt(sales_dist_e, id_vars=['Area', 'Store Name'], var_name='option').set_index(['Area', 'Store Name', 'optionx']).squeeze().unstack().reset_index()
which was mostly derived from this previous related question, but I can't seem to make it work with my current example.