My DataFrame包含以下列和行
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
我想将每一行转换为以下json格式,并向API发出post请求。(请注意,“id”始终为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"
}
]
}
最后,将响应作为新列添加到数据帧中
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
这是解决办法
由于您希望对数据帧中存在的所有行运行此操作,请执行以下操作 向API发送POST请求的步骤您可以使用这个实现
您可以将pandas函数https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_json.html与
df.to_json()
一起应用,然后访问每一行以获得json表示相关问题 更多 >
编程相关推荐