Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Concatenate every value in list to every value in column

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

_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'])]})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading