如何通过读取 csv 数据形成正确的 JSON 并在 API 负载中使用

德维什·乔希

我使用下面的代码来形成一个 JSON,通过从 csv 读取数据

df = pd.read_csv('/testdata.csv', dtype={

 "debt_type": str,
 "debt_amount": int,
 "interest_rate": float,
 "total_monthly_payment": int,
 "remaining_term,interest_payable": int})

finalList = []
finalDict = {}
grouped = df.groupby(['debt_type'])
for key, value in grouped:
    dictionary = {}

    j = grouped.get_group(key).reset_index(drop=True)
    dictionary['debt_type'] = j.at[0, 'debt_type']

    dictList = []
    anotherDict = {}
    for i in j.index:
        anotherDict['debt_amount'] = j.at[i, 'debt_amount']
        anotherDict['interest_rate'] = j.at[i, 'interest_rate']
        anotherDict['total_monthly_payment'] = j.at[i, 'total_monthly_payment']
        anotherDict['remaining_term'] = j.at[i, 'remaining_term']
        anotherDict['interest_payable'] = j.at[i, 'interest_payable']

        dictList.append(anotherDict)
        dictionary['loan_info'] = dictList
        finalList.append(dictionary)
        finalDict = finalList

并希望达到以下目标

{"loan_info":{"debt_amount":9000,"interest_rate":23,"total_monthly_payment":189,"remaining_term":129,"interest_payable":15356},"debt_type":"credit_card"}

然而,我得到的是下面

[{'debt_type': 'credit_card', 'loan_info': [{'debt_amount': 9000, 'interest_rate': 12.2, 'total_monthly_payment': 189, 'remaining_term': 129, 'interest_payable': 15256}]}]

任何人都可以在这里帮忙。提前致谢。

贾斯托尔

我认为你需要的是使用pandas.DataFrame.to_dict()pandas.DataFrame.to_json()

阅读loan_infocsv 文件后,您可以创建一个新列,将您想要的所有字段格式化为 Python 字典:

loan_info_cols = ['debt_amount', 'interest_rate', 'total_monthly_payment', 'remaining_term', 'interest_payable']
df['loan_info'] = df[loan_info_cols].apply(lambda x: x.to_dict(), axis=1)

然后删除我们刚刚使用的列:

df = df.drop(loan_info_cols, axis=1)

这是我们到目前为止所拥有的:

print(df)

     debt_type                                          loan_info
0  credit_card  {u'total_monthly_payment': 189.0, u'interest_p...
1   debit_card  {u'total_monthly_payment': 165.0, u'interest_p...

现在您可以将整个数据帧转换为 JSON :

df_json = df.to_json(orient='records', lines=True)
print(df_json)

{"debt_type":"credit_card","loan_info":{"total_monthly_payment":189.0,"interest_payable":15356.0,"interest_rate":23.0,"debt_amount":9000.0,"remaining_term":129.0}}
{"debt_type":"debit_card","loan_info":{"total_monthly_payment":165.0,"interest_payable":21354.0,"interest_rate":24.0,"debt_amount":8000.0,"remaining_term":167.0}}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

读取 Json 数据并在角度组件中使用它

通过url获取json数据并在python中使用(simplejson)

如何反序列化IPGeoLocation API返回的json并在.chtml视图中使用

用于从csv读取并在mysql查询中使用的bash脚本

如何每 2 分钟从 API 获取数据并在所有项目中使用它?

如何使用JSON正确读取API并创建列表?在Python中

如何将 json 数据转换为特定别名并在 mysql 中使用逗号分隔值

WordPress 插件:如何在 JSON 中获取数据并在 android 中使用?

如何分离json数据的键并在表头中使用它[javascript或PHP]

如何从javascript数组对象/JSON获取数据并在React JS中使用它

如何在Android应用中使用GSON从Youtube API读取JSON-c数据

通过读取csv数据在R中计算t检验,并在Boxplot中将其可视化

如何从json文件中读取值并在MATLAB plot函数中使用它?

从JSON文件获取数据并在C#中使用它

通过API调用在Go中使用JSON数据

如何使用 sh 脚本正确读取文件并在 if 语句中使用它?

如何從 CSV 形成 JSON

如何从Excel电子表格中读取输入数据并在空手道框架中传递JSON有效负载?

如何解码来自WHMCS的json数据并在laravel-5.6中使用foreach循环显示数据

如何将 JSON 数据从服务器传递到客户端并在 Javascript 中使用数据?

如何使用占位符通过 API 发布 Json 元数据

如何使用VBScript通过HTTP API发布JSON数据?

如何使用node.js调用API并在正文中包含JSON字符串?

如何从URL读取数据并在折线图中使用它?收到JAVAFX错误

收集用户的数据,通过PHP并在SQL查询中使用

如何在flutter中使用API调用嵌套的json数据?

如何在反应中使用来自 api (json) 的数据

Java通过CSV过滤并在数据中查找间隙

如何从eBay api获取Json数据以CSV格式保存?