简体   繁体   中英

appending new rows based on conditions python pandas

Need to add new rows to current df, based on conditions.

Current 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 . 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:

在此处输入图像描述

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() :

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

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