【问题标题】:back calculation in excel columns in pythonpython中excel列中的反向计算
【发布时间】:2019-11-29 12:53:40
【问题描述】:

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

ID    Date       Input1    Input2   1.Eff   2.Eff    Qty     Time   
3   1/2/2019        A        A      32.08   76.64     5      200
3   1/3/2019        A        A      55.95   41.18     10     100
3   1/4/2019        A        A      56.61    50        5     300
3   1/4/2019        A        B      56.61   35.67     10     300

在输出字段中,我想要两列 new_Eff 和 new_time 将从上述数据计算,计算 New_time 和 New_eff 的逻辑是: 1,如果ID和Date组合如果有一个行项目,那么1.Eff将等于new_eff,new_time将等于时间。 所以对于第 1 行和第 2 行的输出将是

   ID     Date       Input1    Input2   1.Eff   2.Eff    Qty     Time    new_time    new_EFf
    3   1/2/2019        A        A      32.08   76.64     5      200      32.08       200
    3   1/3/2019        A        A      55.95   41.18     10     100      55.95       100

在第 3 行和第 4 行中,日期没有变化,因此 new_Eff 将等于第 1 天 2.Eff 和时间将等于时间/数量,因此第 3 行输出将是:

ID    Date       Input1    Input2   1.Eff   2.Eff    Qty     Time    new_time    new_EFf 
3   1/4/2019        A        A      56.61   50       5      300     300/5=60     41.48 (on 3rd 2.Eff is 41.48)

在第 4 行 new_time 将是总时间-60=300-60=240 并且 new_eff 将是 new_time/Qty=240/5=48

ID    Date       Input1    Input2   1.Eff   2.Eff    Qty     Time    new_time    new_EFf 
3   1/4/2019        A        B      56.61   35.67     10     300      240          48

所以输出表将如下所示:

  ID      Date       Input1    Input2   1.Eff   2.Eff    Qty     Time    new_time    new_EFf
    3   1/2/2019        A        A      32.08   76.64     5      200      32.08       200
    3   1/3/2019        A        A      55.95   41.18     10     100      55.95       100
    3   1/4/2019        A        A      56.61   50        5      300        60        41.48
    3   1/4/2019        A        B      56.61   35.67     10     300       240          48

当同一日期有多行时,谁能帮助我如何执行这些反向计算..

提前致谢

【问题讨论】:

  • 如果有超过 2 行具有相同的 id 和日期会发生什么?
  • 不会是这样,要么是 1,要么是最大 2-@Dev Khadka

标签: python pandas python-2.7 dataframe metadata


【解决方案1】:

你可以使用下面的 groupby 函数来做到这一点


df = pd.DataFrame([[3, '1/2/2019', 'A', 'A', 32.08, 76.64, 5, 200], [3, '1/3/2019', 'A', 'A', 55.95, 41.18, 10, 100], [3, '1/4/2019', 'A', 'A', 56.61, 50.0, 5, 300], [3, '1/4/2019', 'A', 'B', 56.61, 35.67, 10, 300]], columns=('ID', 'Date', 'Input1', 'Input2', '1.Eff', '2.Eff', 'Qty', 'Time'))

def calc(g):

    if len(g)>1:
        fst = g.iloc[[0]]
        other = g.iloc[1:]
        fst["new_time"] = fst["Time"]/fst["Qty"]
        fst["new_EFf"] = fst["2.Eff"]

        other["new_time"] = other["Time"]-60
        other["new_EFf"] = other["new_time"].values/fst["Qty"].values
        g = pd.concat([fst,other], axis=0)

    else:
        g = g.copy()
        g["new_time"] = g["Time"]
        g["new_EFf"] = g["1.Eff"]

    return g

df.groupby(["ID", "Date"]).apply(calc).reset_index(drop=True)

结果

 ID      Date Input1 Input2  1.Eff  2.Eff  Qty  Time  new_time  new_EFf
0   3  1/2/2019      A      A  32.08  76.64    5   200     200.0    32.08
1   3  1/3/2019      A      A  55.95  41.18   10   100     100.0    55.95
2   3  1/4/2019      A      A  56.61  50.00    5   300      60.0    50.00
3   3  1/4/2019      A      B  56.61  35.67   10   300     240.0    48.00

【讨论】:

    【解决方案2】:

    这是我使用np.select 的方法:

    first = df['Date'] == df['Date'].shift(-1)
    second = first.shift(fill_value=False)
    
    df['new_time'] = np.select((first, second),
                               (df['Time']/df['Qty'], df['Time']-60),
                               df['Time'])
    
    df['new_Eff'] = np.select((first, second),
                              (df['2.Eff'].shift(), df['new_time']/df['Qty']),
                              df['1.Eff']
                             )
    

    输出:

       ID      Date Input1 Input2  1.Eff  2.Eff  Qty  Time  new_time  new_Eff
    0   3  1/2/2019      A      A  32.08  76.64    5   200     200.0    32.08
    1   3  1/3/2019      A      A  55.95  41.18   10   100     100.0    55.95
    2   3  1/4/2019      A      A  56.61  50.00    5   300      60.0    41.18
    3   3  1/4/2019      A      B  56.61  35.67   10   300     240.0    24.00
    

    注意:根据您的问题 在第 4 行中,new_time 的总数将是 time-60=300-60=240new_eff 将是 new_time/Qty=240/5=48 应该是 Qty in row410new_Eff24 如我的回答所示?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-17
      • 1970-01-01
      • 2022-08-17
      • 1970-01-01
      • 2022-01-13
      • 2017-03-01
      • 2022-12-12
      • 2018-09-08
      相关资源
      最近更新 更多