【问题标题】:Percent difference calculation百分比差异计算
【发布时间】:2020-11-27 17:34:38
【问题描述】:

我想知道是否可以简化以下计算或使用替代计算:

id      | group | value
01      |   1   |   5
01      |   1   |   6
02      |   1   |   35
02      |   1   |   40
03      |   1   |   90
03      |   1   |   95
control |   1   |   50
control |   1   |   60
04      |   2   |   35
04      |   2   |   36
05      |   2   |   15
05      |   2   |   10
06      |   2   |   20
06      |   2   |   25
control |   2   |   30
control |   2   |   40

首先计算每组所有对照的平均值:

id      | group | value_mean
control |   1   |   55
control |   2   |   35

然后计算每个id相对于控件的平均差:

id      | group | value_mean_percent
01      |   1   |   10
02      |   1   |   68
03      |   1   |   168
04      |   2   |   101
05      |   2   |   36
06      |   2   |   64

id=1 的示例 (5+6)/2*100/55 = 10 %

还有其他方法可以解释这些数据的结果吗?

【问题讨论】:

    标签: python pandas statistics percentage difference


    【解决方案1】:

    我认为您可以定义一个适用于数据框子组的函数,它将valueid 分组,计算平均值并返回除以控制的所有内容:

    def func(da):
        da = da.groupby('id')['value'].mean()
        return 100*da[da.index != "control"]/da['control']
    

    然后你只需将data.frame按group分组,应用这个函数,然后重置索引:

    import pandas as pd
    import numpy as np
    
    df = pd.DataFrame({"id":np.repeat(['01','02','03','control','04','05','06','control'],2),
                      "group":np.repeat([1,2],8),
                       "value":[5,6,35,40,90,95,50,60,35,36,15,10,20,25,30,40]})
    
    df.groupby('group').apply(func).reset_index()
    
        group   id  value
    0   1   01  10.000000
    1   1   02  68.181818
    2   1   03  168.181818
    3   2   04  101.428571
    4   2   05  35.714286
    5   2   06  64.285714
    
             
    

    【讨论】:

    • 谢谢,不知道可以在pandas中应用函数。
    猜你喜欢
    • 2021-06-17
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-09
    • 2016-07-12
    相关资源
    最近更新 更多