Table 1
| ID | Condition | Access1 | Access2 |
|---|---|---|---|
| 1 | aa | nan | nan |
| 2 | aa | nan | nan |
| 3 | bb | nan | nan |
| 4 | bb | nan | nan |
| 5 | aa | nan | nan |
| 6 | bb | nan | nan |
| 7 | aa | nan | nan |
Table 2
| Name | Condition | Accessor1 | Accessor2 |
|---|---|---|---|
| John | aa | Yes | No |
| Mary | aa | No | Yes |
| Bob | aa | Yes | Yes |
| Ben | bb | Yes | Yes |
| Peter | bb | No | Yes |
Table 1 contains the condition.
Table 2 contains the condition and the name of the accessors with their authorization. Either they can access to 1 or 2 or both.
For example, John can access to 1 but not 2, so we have to match the condition of Table 1. ID 1 condition is 'aa', so John can access1 and his name is not fill in access2 and continue to find for other name who has access to it. Mary's condition is 'aa' and has access to 2 so her name is filled in access2.
Bob's condition is 'aa' and ID 2 is 'aa'. Bob has access to 1 or 2. Hence, his name is filled in ID 2's access1 and access2.
ID 5, since Bob is the last name with condition 'aa'. Hence, it will loop back to John again. Same goes to the rest.
Result Table
| ID | Condition | Access1 | Access2 |
|---|---|---|---|
| 1 | aa | John | Mary |
| 2 | aa | Bob | Bob |
| 3 | bb | Ben | Ben |
| 4 | bb | Ben | Peter |
| 5 | aa | John | Mary |
| 6 | bb | Ben | Ben |
| 7 | aa | Bob | Bob |
My idea is to create a
cyclic listsfor a condition using table 2 and then fill table 1. My solution is mix of use of conditional statements and pandas broadcasting concept.Let take example we want to fill
table 1columnAccess1with conditionaa,table 2with conditionaawe have three name to fill[John, Mary, Bob]Accessor1column condition we are only able filltable 1with[John, Bob]1. Convert Accessor columns of table 2 into
dtype boolso that we can use pandas boolean indexing to create conditons2. Creating lists from which we are allowed to fill the column
3. Converting list to circular list so that we can fill values of column which require more row to fill than what we have in our hand
4. Creating column size list based on condition
4. Finally filling na with above list
Results
Note: Lot of things are here to optimize here, would love to know new solution that full depends upon pandas concept at all.