python - 如何将 pandas 行转换为自定义 json 格式并发出 POST 请求
问题描述
我的 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"
}
]
}
最后将响应作为新列添加到 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
解决方案
您可以应用 pandas 函数https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_json.html并df.to_json()
访问每一行以获取 json 演示文稿。
推荐阅读
- java - LU 分解方法中的线程“main”java.lang.NullPointerException 中的异常
- html - Vue.js - 将数据存储到会话中
- javafx - JavaFX - WebEngine.executeScript() 中的看门狗定时器
- c# - 当有人进入触发器时使物体掉落(Unity 3D)
- python - discord.py 终端命令提示符`input()` 功能阻塞
- android - ConstraintLayout 截断 TextView 中过长的文本
- ios - 从 Objective C 类更新 SwiftUI 视图
- javascript - 保存多次点击的值并内联获取所有值
- sql - 我该如何解决这个问题,以便 raw_visits_sum 计算任何 date_range_start 时间的所有值的总和?
- javascript - 在 Android 上的 Chrome 浏览器中 - 响应辅助功能下的文本缩放选项的字体大小