# Group and sort in descending order of scenario
grp = df.sort_values(['master id', 'scenario'], ascending=False).groupby('master id')
# Get only the latest and previous scenarios
df_first = grp.nth(0).reset_index() # synonymous to .first()
df_first['rev_scen'] = 'current'
df_second = grp.nth(1).reset_index()
df_second['rev_scen'] = 'previous'
df_latest = df_first.append(df_second) # Merge the two
# From here on it's basically the same as the original answer
df_scen = df_latest.groupby(['master id', 'rev_scen']).agg({'price': 'sum'}).unstack('rev_scen')
df_scen['% change'] = 100 * df_scen[('price', 'current')] / df_scen[('price', 'previous')] - 100
因此,第一步是按主id和场景分组,然后取消堆叠场景以获得每个场景的列:
下一步是为%change创建一个新列。您可能需要稍微处理一下列名,但它应该如下所示:
编辑
如果每个主ID有多个方案,并且只需要最新的两个方案:
试试这个:
相关问题 更多 >
编程相关推荐