熊猫多索引Groupby聚合-多层

时间:2020-05-01 15:29:46

标签: python pandas plotly

我试图使用存储在多索引数据框中的Plotly绘制以下数据:

ship_out_df.head(3)
                              out_tonnes    
date        loc     product         
2020-05-01  ABC     AFM       8000
                    PRE       6000
            DEF     PRE       6000
                    BZF       25200
2020-05-02  ABC     AFM       8400  
                    PRE       0 
            DEF     PRE       0 
                    BZF       25700
2020-05-03  ABC     AFM       8000  
                    PRE       8000  
            ABC     PRE       8000  
                    BZF       25000 

不幸的是,Plotly无法处理多索引数据帧,因此不会绘制具有重复索引值(即“ PRE”)的记录。

如何获取值以传递到维护日期/等级结构的绘图参数(我不需要loc)?即“ PRE”

date          product  out_tonnes
'2020-05-01'  PRE      12000
'2020-05-02'  PRE      0
'2020-05-03'  PRE      16000

我尝试过:

y = ship_out_df.groupby(['product']).get_group('PRE').out_tonnes.values

但是,这当然会丢失“日期”结构并计算所有值。

1 个答案:

答案 0 :(得分:0)

IIUC,

result = (df.reset_index().loc[lambda x: x['product'].eq('PRE')]
            .groupby(['date','product'], as_index=False)['out_tonnes']
            .sum())
print(result)

         date product  out_tonnes
0  2020-05-01     PRE       12000
1  2020-05-02     PRE           0
2  2020-05-03     PRE       16000