Monday 28 August 2023

Add dataframe rows based on external condition

I have this data frame:

print(df)

    Env location lob      grid row server        model        make          slot     
    Prod USA     Market   AB3 bc2  Server123     Hitachi        dcs           1        
    Prod USA     Market   AB3 bc2  Server123     Hitachi        dcs           2        
    Prod USA     Market   AB3 bc2  Server123     Hitachi        dcs           3        
    Prod USA     Market   AB3 bc2  Server123     Hitachi.       dcs           4        
    Dev  EMEA    Ins.     AB6 bc4  Serverabc     IBM            abc           3        
    Dev  EMEA    Ins.     AB6 bc4  Serverabc     IBM            abc           4        
    Dev  EMEA    Ins.     AB6 bc4  Serverabc     IBM            abc           5        
    Dev  EMEA    Ins.     AB6 bc4  Serverabc     IBM            abc           6
    UAT  PAC     Retail   AB6 bc4  Serverzzz     Cisco          ust           3        
    UAT  PAC     Retail   BB6 bc4  Serverzzz     Cisco          ust           4        
    UAT  PAC     Retail   BB6 bc4  Serverzzz     Cisco          ust           5        
    UAT  PAC     Retail   BB6 bc4  Serverzzz     Cisco          ust           6

In this example:

  • If model is IBM, slot starts from slot=3 and there must be 8 slots, i.e. go from 3 to 10. In this case, only slots 3 to 6 are present.
    • Therefore, I need to add 4 more rows (slot 7, 8, 9, 10).
  • If model is Cisco, row count for cisco needs to be 6. Only slots 3 to 6 are present.
    • Therefore, I need to add 2 more rows

New rows:

  • must repeat the last row for the model, while incrementing the slot number
  • Their "grid" cell must indicate "available".

This needs to be programatically where given the model, I need to know the total number of slots and if the number of slot is short, I need to create new rows.

The final data frame needs to be like this:

    Env location lob    grid  row server        model        make               slot     
    Prod USA     Market AB3       bc2  Server123     Hitachi        dcs           1        
    Prod USA     Market AB3       bc2  Server123     Hitachi        dcs           2        
    Prod USA     Market AB3       bc2  Server123     Hitachi        dcs           3        
    Prod USA     Market AB3       bc2  Server123     Hitachi.       dcs           4        
    Dev  EMEA    Ins.   AB6       bc4  Serverabc     IBM            abc           3        
    Dev  EMEA    Ins.   AB6       bc4  Serverabc     IBM            abc           4        
    Dev  EMEA    Ins.   AB6       bc4  Serverabc     IBM            abc           5        
    Dev  EMEA    Ins.   AB6       bc4  Serverabc     IBM            abc           6
    Dev  EMEA    Ins.   available bc4  Serverabc     IBM            abc           7
    Dev  EMEA    Ins.   available bc4  Serverabc     IBM            abc           8
    Dev  EMEA    Ins.   available bc4  Serverabc     IBM            abc           9
    Dev  EMEA    Ins.   available bc4  Serverabc     IBM            abc           10
    UAT  PAC     Retail   AB6     bc4  Serverzzz     Cisco          ust           3        
    UAT  PAC     Retail   BB6     bc4  Serverzzz     Cisco          ust           4        
    UAT  PAC     Retail   BB6     bc4  Serverzzz     Cisco          ust           5        
    UAT  PAC     Retail   BB6     bc4  Serverzzz     Cisco          ust           6
    UAT  PAC     Retail  available bc4  Serverzzz     Cisco          ust          7
    UAT  PAC     Retail  available bc4  Serverzzz     Cisco          ust          8

I tried something like this:

def slots(row):
   if 'IBM' in row['model']:
      number_row=8
   if 'Cisco' in row['model']:
      number_row=6

I am not familiar too much with pandas, not even sure something like this could possible.



from Add dataframe rows based on external condition

No comments:

Post a Comment