简体   繁体   English

根据条件追加新行 python pandas

[英]appending new rows based on conditions python pandas

Need to add new rows to current df, based on conditions.需要根据条件向当前 df 添加新行。

Current df:当前df: 在此处输入图像描述

What I want to do is check the Sales Oder and if contains more than one order add new rows to df and update all columns remain as same as the current row but need to update Planned Shift Production(m) and Actual Shift Production(m) based on current row's AvgPlannedShiftProd and AvgActualShiftProd .我想要做的是检查销售订单如果包含多个订单,则将新行添加到 df 并更新所有列与当前行保持相同,但需要更新计划班次生产(m)和实际班次生产(m)基于当前行的 AvgPlannedShiftProd 和 AvgActualShiftProd End goal is to maintaining one Sale Order for each row.最终目标是为每一行维护一个销售订单。

df:东风:

T={'Date': {1195: '2021-05-24',
  1196: '2021-05-24',
  1197: '2021-05-25',
  1198: '2021-05-25',
  1199: '2021-05-26',
  1200: '2021-05-26'},
 'Machine No': {1195: 'M-1',
  1196: 'M-1',
  1197: 'M-1',
  1198: 'M-1',
  1199: 'M-1',
  1200: 'M-1'},
 'Shift': {1195: 'Day',
  1196: 'Night',
  1197: 'Day',
  1198: 'Night',
  1199: 'Day',
  1200: 'Night'},
 'Sales Order': {1195: 'Z21',
  1196: 'A1',
  1197: 'B1-B2-B3-B4-B5',
  1198: 'B1-B2-B3-B4-B5',
  1199: 'B1-B2-B3-B4-B5',
  1200: 'B1-B2-B3-B4-B5'},
 'Quality No': {1195: 'C100',
  1196: 'C100',
  1197: 'C100',
  1198: 'C100',
  1199: 'C100',
  1200: 'C100'},
 'Planned Shift Production (m)': {1195: 0,
  1196: 0,
  1197: 4240,
  1198: 4232,
  1199: 0,
  1200: 0},
 'Actual Shift Production (m)': {1195: 3611,
  1196: 3384,
  1197: 3097,
  1198: 2989,
  1199: 0,
  1200: 0},
 'NoOfSalesOrders': {1195: 1.0,
  1196: 1.0,
  1197: 5.0,
  1198: 5.0,
  1199: 5.0,
  1200: 5.0},
 'AvgPlannedShiftProd': {1195: 0.0,
  1196: 0.0,
  1197: 848.0,
  1198: 846.4,
  1199: 0.0,
  1200: 0.0},
 'AvgActualShiftProd': {1195: 3611.0,
  1196: 3384.0,
  1197: 619.4,
  1198: 597.8,
  1199: 0.0,
  1200: 0.0}}
pd.DataFrame.from_dict(T)

Sample of Expected Output:预期 Output 样品:

在此处输入图像描述

Thnaks in advance !!!!!!!!!!提前谢谢!!!!!!!!!!

I hope I've understood your question right: you can split the Sales Order column, update "Planned Shift Production (m)" , "Actual Shift Production (m)" columns and .explode() :我希望我已经正确理解了您的问题:您可以拆分Sales Order列,更新"Planned Shift Production (m)""Actual Shift Production (m)"列和.explode()

df["Sales Order"] = df["Sales Order"].str.split("-")
df[["Planned Shift Production (m)", "Actual Shift Production (m)"]] = df[
    ["AvgPlannedShiftProd", "AvgActualShiftProd"]
]

print(df.explode("Sales Order"))

Prints:印刷:

            Date Machine No  Shift Sales Order Quality No  Planned Shift Production (m)  Actual Shift Production (m)  NoOfSalesOrders  AvgPlannedShiftProd  AvgActualShiftProd
1195  2021-05-24        M-1    Day         Z21       C100                           0.0                       3611.0              1.0                  0.0              3611.0
1196  2021-05-24        M-1  Night          A1       C100                           0.0                       3384.0              1.0                  0.0              3384.0
1197  2021-05-25        M-1    Day          B1       C100                         848.0                        619.4              5.0                848.0               619.4
1197  2021-05-25        M-1    Day          B2       C100                         848.0                        619.4              5.0                848.0               619.4
1197  2021-05-25        M-1    Day          B3       C100                         848.0                        619.4              5.0                848.0               619.4
1197  2021-05-25        M-1    Day          B4       C100                         848.0                        619.4              5.0                848.0               619.4
1197  2021-05-25        M-1    Day          B5       C100                         848.0                        619.4              5.0                848.0               619.4
1198  2021-05-25        M-1  Night          B1       C100                         846.4                        597.8              5.0                846.4               597.8
1198  2021-05-25        M-1  Night          B2       C100                         846.4                        597.8              5.0                846.4               597.8
1198  2021-05-25        M-1  Night          B3       C100                         846.4                        597.8              5.0                846.4               597.8
1198  2021-05-25        M-1  Night          B4       C100                         846.4                        597.8              5.0                846.4               597.8
1198  2021-05-25        M-1  Night          B5       C100                         846.4                        597.8              5.0                846.4               597.8
1199  2021-05-26        M-1    Day          B1       C100                           0.0                          0.0              5.0                  0.0                 0.0
1199  2021-05-26        M-1    Day          B2       C100                           0.0                          0.0              5.0                  0.0                 0.0
1199  2021-05-26        M-1    Day          B3       C100                           0.0                          0.0              5.0                  0.0                 0.0
1199  2021-05-26        M-1    Day          B4       C100                           0.0                          0.0              5.0                  0.0                 0.0
1199  2021-05-26        M-1    Day          B5       C100                           0.0                          0.0              5.0                  0.0                 0.0
1200  2021-05-26        M-1  Night          B1       C100                           0.0                          0.0              5.0                  0.0                 0.0
1200  2021-05-26        M-1  Night          B2       C100                           0.0                          0.0              5.0                  0.0                 0.0
1200  2021-05-26        M-1  Night          B3       C100                           0.0                          0.0              5.0                  0.0                 0.0
1200  2021-05-26        M-1  Night          B4       C100                           0.0                          0.0              5.0                  0.0                 0.0
1200  2021-05-26        M-1  Night          B5       C100                           0.0                          0.0              5.0                  0.0                 0.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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