【问题标题】:subtract 1 from next cumsum if current cumsum more than a particular value - pandas or numpy如果当前 cumsum 大于特定值,则从下一个 cumsum 中减去 1 - pandas 或 numpy
【发布时间】:2020-08-05 09:47:54
【问题描述】:

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

B_ID    Session    no_show   cumulative_no_show
1       s1         0.4       0.4
2       s1         0.6       1.0
3       s1         0.2       1.2
4       s1         0.1       1.3
5       s1         0.4       1.7
6       s1         0.2       1.9
7       s1         0.3       2.2
10      s2         0.3       0.3
11      s2         0.4       0.7
12      s2         0.3       1.0
13      s2         0.6       1.6
14      s2         0.2       1.8
15      s2         0.5       2.3

其中,cumulative_no_show 是 no_show 的累积和。

根据以上条件,我想创建一个名为 u_no_show 的新列。

每当累积无显示 >= 0.8 时,从下一个累积无显示中减 1。等等。

预期输出:

B_ID    Session    no_show   cumulative_no_show   u_no_show
1       s1         0.4       0.4                  0.4
2       s1         0.6       1.0                  1.0
3       s1         0.2       1.2                  0.2
4       s1         0.1       1.3                  0.3
5       s1         0.4       1.7                  0.7
6       s1         0.2       1.9                  0.9
7       s1         0.3       2.2                  0.2
10      s2         0.3       0.3                  0.3
11      s2         0.4       0.7                  0.7
12      s2         0.3       1.0                  1.0
13      s2         0.6       1.6                  0.6
14      s2         0.2       1.8                  1.8
15      s2         0.5       2.3                  0.3

【问题讨论】:

    标签: pandas numpy pandas-groupby numba


    【解决方案1】:

    我假设您想在每个会话中执行此操作。我不确定是否有矢量化解决方案,所以我会创建一个迭代值并在需要时进行减法的函数,然后使用groupby.apply

    def create_u_no_show (ser):
        # convert to numpy aray and iterate
        arr_ns = ser.to_numpy()
        for i in range(len(arr_ns)-1):
            # check if the condition is met
            if arr_ns[i]>0.8:
                # remove 1 to all the next values if the condition is met
                arr_ns[i+1:] -= 1
        # return a serie with the right index
        return pd.Series(arr_ns, ser.index)
    
    df['u_no_show'] = df.groupby(['Session'])['cumulative_no_show'].apply(create_u_no_show)
    
    print (df) 
        B_ID Session  no_show  cumulative_no_show  u_no_show
    0      1      s1      0.4                 0.4        0.4
    1      2      s1      0.6                 1.0        1.0
    2      3      s1      0.2                 1.2        0.2
    3      4      s1      0.1                 1.3        0.3
    4      5      s1      0.4                 1.7        0.7
    5      6      s1      0.2                 1.9        0.9
    6      7      s1      0.3                 2.2        0.2
    7     10      s2      0.3                 0.3        0.3
    8     11      s2      0.4                 0.7        0.7
    9     12      s2      0.3                 1.0        1.0
    10    13      s2      0.6                 1.6        0.6
    11    14      s2      0.2                 1.8        0.8
    12    15      s2      0.5                 2.3        1.3
    

    【讨论】:

    猜你喜欢
    • 2019-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-22
    • 2018-08-16
    相关资源
    最近更新 更多