I have a list of strings that I’d like to concatenate to every value in a column. Basically if there are 20 values in the column and 5 in the list it would now have 100 rows. The list of tenant IDs comes a thousand lines up in the code, but it’s basically every unique value in the tenantid column. Using dummy data below for propietary reasons.
tenant_ids = ["abc", "def", "ghi"]
tids = []
for t in tenant_ids:
day_1_rules['_id'] = t+'-'+day_1_rules['_id']
tids.append(day_1_rules)
day_1_rules = pd.concat(tids)
After running this it concatenated every value in the list so it was like abc-def-ghi-rule instead of
abc-rule
def-rule
ghi-rule
| _id |
|---|
| Rule_1 |
| Rule_2 |
| Rule_3 |
What I got
| _id |
|---|
| abc-def-ghi-Rule_1 |
| abc-def-ghi-Rule_2 |
| abc-def-ghi-Rule_3 |
What it should be
| _id |
|---|
| abc-Rule_1 |
| abc-Rule_2 |
| abc-Rule_3 |
| def-Rule_1 |
| def-Rule_2 |
| def-Rule_3 |
| ghi-Rule_1 |
| ghi-Rule_2 |
| ghi-Rule_2 |
>Solution :
from itertools import product
tenant_ids = ["abc", "def", "ghi"]
day_1_rules = pd.DataFrame({'_id': ['Rule_1', 'Rule_2', 'Rule_3']})
pd.DataFrame({'_id': [f'{tid}-{rule}' for tid,rule in product(tenant_ids, day_1_rules['_id'])]})