根据列值pandas汇总行

时间:2018-11-30 07:12:23

标签: python pandas pandas-groupby

我有以下格式的数据:

Company     Region  Category    Metric    Year  Month   Value
Industry    Total   NARTD   Sales Value   2017  Jan     1.448129e+09
Industry    Total   NARTD   Sales Volume  2017  Jan     3.573664e+08
Industry    Total   NARTD   Sales Value   2018  Jan     1.422279e+09
Industry    Total   NARTD   Sales Volume  2018  Jan     3.492432e+08

我想在每行的末尾添加另一列,其销售额/销量与其他各列数据相同,但年份除外。需要对同年的销售额和销量进行汇总。

输出:

Company     Region  Category    Metric    Year  Month   Value         AVG
Industry    Total   NARTD   Sales Value   2017  Jan     1.448129e+09  Sal.val(2017)/Sal.Vol(2017)
Industry    Total   NARTD   Sales Volume  2017  Jan     3.573664e+08  Sal.val(2017)/Sal.Vol(2017)
Industry    Total   NARTD   Sales Value   2018  Jan     1.422279e+09  Sal.val(2018)/Sal.Vol(2018)
Industry    Total   NARTD   Sales Volume  2018  Jan     3.492432e+08  Sal.val(2018)/Sal.Vol(2018)

案例

Region  Category    Company     Metric      Year    Month   Value
Convenience     NARTD   TCC     Sales Value 2018    Dec     NaN
Traditional     NARTD   TCC     Sales Value 2018    Dec     NaN
Total           NARTD   TCC     Sales Value 2018    Dec     NaN
Hyper/Super     NARTD   TCC     Sales Value 2018    Dec     NaN

1 个答案:

答案 0 :(得分:0)

IIUC,最简单的方法是首先将每年的所有值都放在一行上,然后进行除法,然后融化以重塑为原始框架:

#10. Return

        min = round((UT - int(UT))*60,0)

#add this after min calculate

        if min == 60:
            hr += 1
            min = 0