简体   繁体   中英

Reg: Pandas Data Frame running sum

We have below dataset it contains about order and task related details as below.

Order  no.  Task NO    Task Start Dt    Task End Dt  task_Completed_Days
A-10012     A-M-202     7-21-2021        7-23-2021        2
A-10012     A-IA-11272  06-07-2021       6-21-2021        14
A-10012     A-CO-10013  7-13-2021        7-16-2021        3
A-10013     A-AB-10026  06-03-2021       6-17-2021        14
A-10013     A-AP-13708  06-03-2021       6-7-2021         4

So by making use of Task start Dt and Task end Dt we created a calculated field Task complete Days Using pandas Data frame. In the same way we have to create Order completion days as below, can someone help how we can find the order completion days using pandas Data frame.

Order  no.  Task NO      Task Start Dt  Task End Dt Task_Completed_Days Order Completed Date
A-10012      A-M-202       7-21-2021    7-23-2021          2               19
A-10012      A-IA-11272   06-07-2021    6-21-2021          14              19
A-10012      A-CO-10013   7-13-2021     7-16-2021          3               19
A-10013      A-AB-10026   06-03-2021    6-17-2021          14              18
A-10013      A-AP-13708   06-03-2021    6-7-2021           4               18

Thanks,

Use groupby_transform :

df['Order Completed Date'] = \
    df.groupby('Order  no.')['task_Completed_Days'].transform('sum')
print(df)

# Output
  Order  no.     Task NO  ... task_Completed_Days Order Completed Date
0    A-10012     A-M-202  ...                   2                   19
1    A-10012  A-IA-11272  ...                  14                   19
2    A-10012  A-CO-10013  ...                   3                   19
3    A-10013  A-AB-10026  ...                  14                   18
4    A-10013  A-AP-13708  ...                   4                   18

[5 rows x 6 columns]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM