【问题标题】:Python | Groupby | Pivot | Using Percentages蟒蛇 |分组 |枢轴 |使用百分比
【发布时间】:2018-05-06 05:47:27
【问题描述】:

我目前正在使用比率,但遇到了障碍。

以下是我正在使用的数据的一小部分样本 [还有其他平台,如 Instagram、Messenger 等],以提供以下更多背景信息:

Date        Reach   Impressions Clicks  Landing_Page    Platform
30/05/2017  27447   27939       90      68              Facebook
30/05/2017  24299   24318       80      44              Facebook
30/05/2017  9897    10081       33      25              Facebook
30/05/2017  11696   11721       33      21              Facebook
30/05/2017  53      55          1                       Facebook
31/05/2017  46632   68757       213     143             Facebook
31/05/2017  67478   73401       650     424             Facebook
31/05/2017  38831   47577       136     77              Facebook
31/05/2017  46834   52449       135     77              Facebook
31/05/2017  273     531         12      10              Facebook
1/06/2017   48307   72141       221     150             Facebook
1/06/2017   64122   79501       202     106             Facebook
1/06/2017   66810   71033       843     575             Facebook
1/06/2017   46225   50003       138     76              Facebook
1/06/2017   496     1043        16      15              Facebook

然后我创建了一个新列:

df["Click_To_Landing_Ratio] = df["Landing_Page] / df["Clicks"] * 100

我尝试了以下方法:

round(df.pivot_table(index="Date", columns="Platform", values="Click_To_Landing_Ratio"), 3)

round(df.groupby(["Date", "Platform"], axis=0)["Click_To_Landing_Ratio"].mean().unstack(), 3)

当我在 excel 中旋转它(并创建一个计算列)时,我得到

Row Labels  Facebook    Grand Total
30/05/2017  66.67%      66.67%
31/05/2017  63.79%      63.79%
1/06/2017   64.93%      64.93%
2/06/2017   63.98%      63.98%

当我在 Pandas 中 Pivot 或 Groupby 时,我得到:

Row Labels  Facebook    Grand Total
30/05/2017  53.990%     53.990% 
31/05/2017  65.871%     65.871% 
1/06/2017   67.476%     67.476%
2/06/2017   64.031%     64.031%

我猜 Pandas 正在平均当天的行值,而 Excel 将创建一个总数并将其除以总条目(如果这有意义的话)。

简而言之,我的问题是:

  1. 如果您在每一行都有比率(百分比),有没有办法在 Excel 中复制相同的结果,但在 Pandas 中?
  2. 您能否使用类似于 Pandas 的 groupby 获得总数(margins=True)?

任何帮助将不胜感激。

【问题讨论】:

    标签: python pandas group-by pivot-table


    【解决方案1】:

    您已正确识别的问题是您无意中平均了每日点击到着陆值,而 Excel 是通过将每天的点击次数和着陆页访问的值相加并除以计算平均值。

    您可以在pandas 中执行相同的操作,方法是创建您的pivot_table 并对每天的值求和(注意我已将aggfunc='sum' 传递给pivot_table)。然后,您可以应用除法来求平均值:

    df
              Date  Reach  Impressions  Clicks  Landing_Page  Platform
    0   30/05/2017  27447        27939      90            68  Facebook
    1   30/05/2017  24299        24318      80            44  Facebook
    2   30/05/2017   9897        10081      33            25  Facebook
    3   30/05/2017  11696        11721      33            21  Facebook
    4   30/05/2017     53           55       1             0  Facebook
    5   31/05/2017  46632        68757     213           143  Facebook
    6   31/05/2017  67478        73401     650           424  Facebook
    7   31/05/2017  38831        47577     136            77  Facebook
    8   31/05/2017  46834        52449     135            77  Facebook
    9   31/05/2017    273          531      12            10  Facebook
    10   1/06/2017  48307        72141     221           150  Facebook
    11   1/06/2017  64122        79501     202           106  Facebook
    12   1/06/2017  66810        71033     843           575  Facebook
    13   1/06/2017  46225        50003     138            76  Facebook
    14   1/06/2017    496         1043      16            15  Facebook
    
    t = df.pivot_table(index="Date", columns="Platform", values=['Clicks', 'Landing_Page'], aggfunc='sum')
                 Clicks Landing_Page
    Platform   Facebook     Facebook
    Date                            
    1/06/2017      1420          922
    30/05/2017      237          158
    31/05/2017     1146          731
    
    t[('Landing_Page', 'Facebook')].div(t[('Clicks', 'Facebook')]).apply(lambda x: '{:.2%}'.format(x))
    Date
    1/06/2017     64.90%
    30/05/2017    66.70%
    31/05/2017    63.80%
    dtype: object
    

    【讨论】:

      猜你喜欢
      • 2017-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多