如何使用Python在JSON中转换其列名作为键和行数据作为值的数据帧的行数据 [英] How to convert the row-wise data of dataframe with its column name as key and row data as value in json using python

查看:223
本文介绍了如何使用Python在JSON中转换其列名作为键和行数据作为值的数据帧的行数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将列名作为键并将行数据作为值转换为数据帧的行数据时,我遇到了问题.我想将此按行json传递给另一个API作为输入.我无法获取此json数据.

I am having the issue in converting the row-wise data of dataframe with the column name as key and row data as value. I want to pass this row-wise json to another API as an input. I am unable to get this json data.

for row in df.iterrows():
    out =row.to_json(orient='records',force_ascii=False)[1:-1].replace('},{', '} {')
    r = requests.post(url, headers=headers, data=out.encode('utf-8'))
    print(r.json())

我想要附件格式的json的所需输出

I want the desired output of the json in the attached format

推荐答案

尝试一下:

import requests
import pandas as pd
def call_api(json_data):
    r = requests.post(url, headers=headers, data=out)
    print(r.json())

df.apply(lambda x:call_api(x.to_json()),axis=1)

希望这会有所帮助!

这篇关于如何使用Python在JSON中转换其列名作为键和行数据作为值的数据帧的行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆