1

I need to pass a comma seperated string into an api.

currently df looks like this:

id      tags
1       ['dc','independent_press','exclusive_variants','marvel']

The API docs for tags found here say that I need to pass in a string like so:

'comics,marvel,dc,batman'

How can I convert the list in the tags column into a string so the df looks like this:

id      tags
1       'comics,marvel,dc,batman'

I would like to pass this string into the api like so:

for index, row in df.iterrows():
            params = {
                'id': row['id'],
                'tags':row['tag'], 
                }
            r = requests.post(url, data=params, headers=headers)
            print(r)

I tried a string replace like so:

df.tags.str.replace('[',"'")

but got result:

0   NaN

edit:

dataframe head in dict:

{'Email': {0: 'test@test.com'},
 'subcategory': {0: 'comics'},
 'category': {0: 'comic'},
 'id': {0: '1'},
 'is_new': {0: 1},
 'tag': {0: 'dc, independent_press, exclusive_varients, marvel'}}
RustyShackleford
  • 3,462
  • 9
  • 40
  • 81

1 Answers1

2

It looks like tags is just a list of str, so this should do the trick:

for index, row in df.iterrows():
    params = {
        'id': row['id'],
        'tags': ','.join(row['tags']), 
    }
    print(params)

    r = requests.post(url, data=params, headers=headers)
    print(r)
dtanabe
  • 1,611
  • 9
  • 18