1

I have some pandas Dataframes with repair order information, with each order containing several criteria c:

orders (o)
------
id c1    c2    c3   c4     c5
0  Car   XL    4    Blue   Metal                                       
1  Jet   XXL   0    Pink   Metal                                       
2  Car   M     4    Blue   Metal                                       
3  Bike  Med   2    Red    Wood            

There is also another list with repair plants. Each plant can have certain criteria c which makes them available only for orders with these criteria. No entry/NaN means that all orders are eligible for repair:

repair_plants (rp)
-------------
id Type  c2    c3
0  Car   NaN   4     --> Available for all Cars with 4 wheels
1  Car   M     4     --> Available for all Cars of size M with 4 wheels
2  Jet   NaN   0     --> Available for all Jets without wheels

rp is generated during runtime, it does not necessarily contain all criteria. A missing criteria should be handled as if it was NaN. I do not know beforehand how many colums with criteria the Repair Plants List has.

The goal is to find a list with all eligble repair plants per order. So for example for order 0 and 2, I would get:

Repair Plants for order 0           Repair Plants for order 2
-------------                       ------------- 
id Type  c2    c3                   id Type  c2    c3
1  Car   M     4                    0  Car   NaN   4                    
(0 is not eligble since c2="XL")    1  Car   M     4                    

What is the best way to create these sublists? I know I can just use .loc and some comparisons if I know all the columns in rp beforehand, e.g.:

for i in range(len(o)):
   value2 = o.loc[i,"c2"] 
   value3 = o.loc[i,"c3"] 
   print(rp[(rp.c2.isna()) | (rp.c2 == value2)) & ((rp.c3.isna()) | (rp.c3 == value3)])

But what do I do to adress the unknown number of criteria? Another loop inside my for loop? What would be the fastest and/or most elegant way of getting the list for each order?

Jan T.
  • 55
  • 1
  • 7
  • maybe you can check this thread: https://stackoverflow.com/questions/45925327/dynamically-filtering-a-pandas-dataframe – Irma May 03 '22 at 20:29

0 Answers0