python - 在 python 中的 excel 列中进行反向计算

标签 python pandas python-2.7 dataframe metadata

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

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的组合,如果有一个line item那么1.Eff等于new_eff,new_time等于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 将等于 Day-1 2.Eff,时间将等于 Time/Qty,因此第 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

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

提前致谢

最佳答案

你可以像下面这样使用 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

关于python - 在 python 中的 excel 列中进行反向计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59105231/

相关文章:

python - 迭代期间在同一行打印字典的值和下一个值

python - 左连接表(1 :n) using Pandas, 保持行数与左表相同

python - Pandas - 按任何列中的最高单个值对数据框进行排序

python - 如何强制 'mkproject' (virtualenvwrapper) 默认使用 python3?

python - 使用 findall 、 Lxml 迭代 Xml

python 列表列表插入换行符

python - 使用 Beautiful Soup 提取链接的等效正则表达式

python - 使用 pandas 将列值转换为行

python - 如何获取模块中的所有变量,但不包括在该模块中导入的变量

python - 重新排序堆叠数据框