我有一个 Dataframe ,它有水平标识符(yes和no)和值,我想把它融化成垂直值,然后把它转化成每个yes。
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
需要得到的答案是:
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
实际上,我需要拆分客户选项表,将相同的利润分配给选择“是”的所有选项,并将选择“否”的所有选项丢弃。
到目前为止,我使用的函数是:e1 = pd.melt(sales_dist_e, id_vars=['Area', 'Store Name'], var_name='option').set_index(['Area', 'Store Name', 'optionx']).squeeze().unstack().reset_index()
,它主要是从this previous related question派生而来的,但是我似乎不能使它与我当前的示例一起工作。
1条答案
按热度按时间4sup72z81#
IIUC,这有用吗?
输出量: