My DataFrame contains the following columns and rows
hosp doc dep p_t ins tpa p_n i_c t_date cat c_amt
ALZ Dr.M Onc SAICO SAISA AZBRONZE AZS 11 2020-08-11 Cons 341.25
ALZ Dr.K Card Mitra Mit ASGOLD ASG 8265 2020-08-15 Cons 1123.45
I want to convert each row to the following json format and make a post request to an API. (Please note that the "id" will always be 1)
{
"hosp": "ALZ",
"doc": "Dr.M",
"dep": "Onc",
"p_t": "SAICO",
"ins": "SAISA",
"tpa": "AZBRONZE",
"p_n": "AZS",
"activities": [
{
"id": "1",
"i_c": "11",
"t_d": "2020-08-11",
"cat": "Cons",
"c_amt": "341.25"
}
]
}
Finally add the response as a new column to the DataFrame
hosp doc dep p_t ins tpa p_n i_c t_date cat c_amt response
ALZ Dr.M Onc SAICO SAISA AZBRONZE AZS 11 2020-08-11 Cons 341.25 Yes
ALZ Dr.K Card Mitra Mit ASGOLD ASG 8265 2020-08-15 Cons 1123.45 No
You can apply pandas function https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_json.html with df.to_json()
and then access each row to get the json presentation.
Here is solution
Since you would want to run this for all the rows present in the DataFrame, do the following# Number of rows all_rows = len(df) for i in range(all_rows): # choose the first half of cols before "activities" x = dict(df.iloc[i, 0:7]) # add the "activites" with the rest of the cols x["activities"] = [dict(df.iloc[i, 7:])] # dict has single quotes which throws an error if sent to an API, so use json.dumps x = json.dumps(x)
To send a POST request to an API
url = 'http://your_api/api/v1/response' headers = { 'content-type': "application/json" } response = requests.request("POST", url, data=x, headers=headers) res = json.loads(response.content)
You can use this implementation,
columns = ['i_c', 't_date', 'cat', 'c_amt']
activities = df[columns]
df.drop(columns=columns, inplace=True)
activities['id'] = '1'
activities_json = eval(activities.to_json(orient='records'))
df['activities'] = activities_json
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.