【问题标题】:Pandas: aggregate and show percent difference熊猫:汇总并显示百分比差异
【发布时间】:2019-08-08 00:23:37
【问题描述】:

我有一个如下所示的数据框:

df = pd.DataFrame( [
  ['BILING',2017,7,1406 ],
  ['BILWPL',2017,7,199],
  ['BKCLUB',2017,7,9417],
  ['LEAVEN',2017,7,4773 ],
  ['MAILORDER',2017,7,10487]
], columns=['Branch','Year','Month','count']

df
Out[1]:
            Branch  Year      Month  count
    0       BILING  2017      7   1406 
    1       BILWPL  2017      7   199
    2       BKCLUB  2017      7   9417
    10      LEAVEN  2017      7   4773 
    18   MAILORDER  2017      7  10487

它包含相同的月份但不同的年份,以便人们可以跨时间比较一年中的时间。

所需的输出如下所示:

    Branch     Month  2017   2019  Mean(ave) percent_diff  
    BILING     7      1406   1501  1480      5%
    BILWPL     7      199    87    102       -40%
    BKCLUB     7      9417   8002  7503      -3%
    LEAVEN     7      4773   5009  4509      -15%
    MAILORDER  7      10487  11032 9004      8%

我的问题是如何基于分支进行聚合以显示并添加 2 列:平均和最新年份之间的平均值和百分比差异。

**** 更新 **** 这很接近,但缺少一些列 [感谢 G. Anderson]:

df.pivot_table(
    values='count', index='Branch', columns='Year',
    fill_value=0, aggfunc='mean')

生产:

Year    2017    2018    2019
Branch          
BILING  1406    1280    4
BILWPL  199     117     239
BKCLUB  94      161     238

这非常接近,但我希望添加对应于平均值和百分比差异的列。

* 更新 2 *

circ_pivot = df.pivot_table(
    values='count', index='Branch', columns='Year',
    fill_value=0)

circ_pivot['Mean'] = circ_pivot[[2017,2018,2019]].mean(axis=1)
circ_pivot['Change'] = ((circ_pivot[2019] - circ_pivot[2018]) / circ_pivot[2018]) * 100
circ_pivot['Change_mean'] = ((circ_pivot[2019] - circ_pivot['Mean']) / circ_pivot['Mean']) * 100

输出:

Year         2017   2018   2019          Mean      Change  Change_mean
Branch                                                                
BILING       1406   1280      4    896.666667  -99.687500   -99.553903
BILWPL        199    117    239    185.000000  104.273504    29.189189
BKCLUB         94    161    238    164.333333   47.826087    44.827586

【问题讨论】:

  • How to pivot a dataframe的可能重复
  • 所有年份之间或随后各年份之间的平均百分比差异?
  • 最新年份与所有年份平均值之间的百分比差异。另外 - 前一年。
  • @G.Anderson -- 显示是正确的,但我还没有遇到添加列的示例......平均百分比差异。谢谢!
  • 您可以将枢轴保存为数据框并正常向其添加列,如this answer

标签: python pandas


【解决方案1】:

这是我最终得到的解决方案。

circ_pivot = df.pivot_table(
    values='count', index='Branch', columns='Year',
    fill_value=0, aggfunc=np.sum, margins=True)

circ_pivot['Mean'] = round(circ_pivot[[2017,2018,2019]].mean(axis=1))
circ_pivot['Change'] = round(((circ_pivot[2019] - circ_pivot[2018]) / circ_pivot[2018]) * 100)
circ_pivot['Change_mean'] = round(((circ_pivot[2019] - circ_pivot['Mean']) / circ_pivot['Mean']) * 100)

print(circ_pivot)

输出:

Year          2017    2018    2019       All      Mean  Change  Change_mean
Branch                                                                     
BILING        1406    1280       4    2690.0     897.0  -100.0       -100.0
BILWPL         199     117     239     555.0     185.0   104.0         29.0
BKCLUB          94     161     238     493.0     164.0    48.0         45.0

改进将是:

  • 相对日期而不是硬编码日期字段。

【讨论】:

    猜你喜欢
    • 2021-01-05
    • 1970-01-01
    • 2023-03-03
    • 2022-11-29
    • 1970-01-01
    • 1970-01-01
    • 2021-02-23
    • 1970-01-01
    • 2022-11-17
    相关资源
    最近更新 更多