So I have a dataframe in pandas that consists of a column with Components, and a row with Constraints. These constraints decide in what category the components have to be filtered. Now these constraints are not very straight-forward, so I'm looking for a way to split them into multiple smaller, more readable constraints. So for example if a constraint is 'A and (B or C)', I want to split up into two rows 'A and B' and 'A and C'. Not all constraints are as easy as this example though.
Here's what a small selection of the dataframe might look like:
Componenent | Constraint |
---|---|
123 | A and (B or C) |
456 | ((MIRROR='ELECTRIC' and MIRRORCAMERA!='NO') or (MIRROR='MANUAL' and (MIRROR_RIGHT!='NO' or MIRROR_LEFT!='NO'))) and STEERWHEEL_LOCK='NO' |
789 | LENGTH='122' or (LENGTH='135' and BATTERY='551') or LENGTH='149' or (LENGTH='181' and (BATTERY='674' or (BATTERY='551' and CHARGER!='NO'))) |
or
import pandas as pd
dataex = {'Component': [123,
456,
789],
'Constraint': ["A and (B or C)",
"((MIRROR='ELECTRIC' and MIRRORCAMERA!='NO') or (MIRROR='MANUAL' and (MIRROR_RIGHT!='NO' or MIRROR_LEFT!='NO'))) and STEERWHEEL_LOCK='NO'",
"LENGTH='122' or (LENGTH='135' and BATTERY='551') or LENGTH='149' or (LENGTH='181' and (BATTERY='674' or (BATTERY='551' and CHARGER!='NO')))"]}
df_example = pd.DataFrame(data=dataex)
Like I said, I'm hoping to split all these into multiple rows (if needed), depending on the and's and or's and parenthesis in the constraint. So I have the following result in mind:
Component | Constraint |
---|---|
123 | A and B |
123 | A and C |
456 | STEERWHEEL_LOCK='NO' and MIRROR='ELECTRIC' and MIRRORCAMERA!='NO' |
456 | STEERWHEEL_LOCK='NO' and MIRROR='MANUAL' and MIRROR_RIGHT!='NO' |
456 | STEERWHEEL_LOCK='NO' and MIRROR='MANUAL' and MIRROR_LEFT!='NO' |
789 | LENGTH='122' |
789 | LENGTH='135' and BATTERY='551' |
789 | LENGTH='149' |
789 | LENGTH='181' and BATTERY='674' |
789 | LENGTH='181' and BATTERY='551' and CHARGER!='NO' |
or
import pandas as pd
datares = {'Component':[123, 123, 456, 456, 456, 789, 789, 789, 789, 789],
'Constraint':["A and B",
"A and C",
"STEERWHEEL_LOCK='NO' and MIRROR='ELECTRIC' and MIRRORCAMERA!='NO'",
"STEERWHEEL_LOCK='NO' and MIRROR='MANUAL' and MIRROR_RIGHT!='NO'",
"STEERWHEEL_LOCK='NO' and MIRROR='MANUAL' and MIRROR_LEFT!='NO'",
"LENGTH='122'",
"LENGTH='135' and BATTERY='551'",
"LENGTH='149'",
"LENGTH='181' and BATTERY='674'",
"LENGTH='181' and BATTERY='551' and CHARGER!='NO'"
]}
df_result = pd.DataFrame(data=datares)
I've tried splitting the constraints on 'or' and dividing them into arrays and then looping over them to get the result, but with some of the more difficult constraints, you get arrays inside arrays inside arrays and then it gets very messy after a while. I've also tried making a sort of logic tree, but I haven't gotten that to work in Python yet.
I'm hoping some of you might have a good idea or module to help my with my problem. Thanks!