Reputation: 69
I have a variable called payload with contains different key value pairs. I need to get values from a dataframe and replace the values in the payload variable. The payload variable is used to pass data to an api call so it needs to follow a structure as shown below:
payload = "{\"title\":\"Enter Title Here\",\"id\":\"1\",\"body\":\"<p>This is a blog post.</p>\",\"author\":\"Vish \",\"thumbnail_path\":\"sample-post.jpg\",\"is_published\":true,\"published_date\":\"Fri, 6 Sep 2019 12:55:31 +0000\",\"tags\":[\"Blog\",\"Example\"]}"
The values for the title, id, body etc are obtained from a dataframe (which is orgianally a csv file).
Title | id | body | author | thumbnail | is_published |
---|---|---|---|---|---|
Enter Title Here | 1 | <p>This is a blog post.</p> |
Vish | sample-post.jpg | true |
Second Title | 2 | <p>2nd blog post.</p> |
User 2 | sample-post.jpg | true |
I am trying to run a for loop where for every row in the df, the values of title, id in the payload variable are updated
df = pd.read_csv('data.csv')
for row in df:
# I need to update the title, id etc in Payload
payload[title]=row.title.to_String
payload[id]=row.id.to_String
payload[body]=row.body.to_String
payload[author]=row.author.to_String
# The above does not work but I want to know the best way to achieve this.
make_post_request(payload, headers) #function to make the api calls
Upvotes: 1
Views: 102
Reputation: 11415
You could aggregate rows to their json representation as that seems to be what you want. Limit first to the desired columns, aggregate, and iterate on the result:
>>> for payload in df[['title', 'id', 'body', 'author']].agg(pd.Series.to_json, axis='columns'):
... print(payload)
...
{"title":"Enter Title Here","id":"1","body":"<p>This is a blog post.<\/p>","author":"Vish "}
So instead of print
you could use your make_post_request
function and you’re done.
If in fact your payload contains other information that you want to keep, you still probably want to handle python objects instead of strings. You can do that by aggregating with to_dict
in a similar way:
>>> import json
>>> template = json.loads(payload)
>>> for entry in df[['title', 'id', 'body', 'author']].agg(pd.Series.to_dict, axis='columns'):
... make_post_request(json.dumps({**template, **entry}), headers)
Upvotes: 1