How can I replace comma separated ids in a string by doing a lookup in json response in python

You can use pandas with isin:

>>> import pandas as pd
>>> df = pd.DataFrame(dealfields)
>>> df
    id label
0  214    BE
1  215    CH
2  274    DE
3  216    ES
4  416    EU
5  218    GB
6  278    HR
7  418    US
>>> data = [{

  "target_country": "214,216,278,418"
}]
>>> fields = df[df['id'].isin(map(int,data[0]["target_country"].split(',')))
                ]['label'].tolist()
>>> target_country = ', '.join(fields)
>>> target_country
'BE, ES, HR, US'

Leave a Comment