0

我的数据来自这样的数据库:

帐户 姓名 地址1 状态 压缩 贷款类型 到期
100 山姆 街 5 纽约 NY001 2019
100 山姆 街 5 纽约 NY001 2020
100 山姆 街 10 新泽西州 NJ001 2019
100 山姆 街 10 新泽西州 NJ001 2020
101 约翰 街道 1 加州 CA001 2019
101 约翰 街道 1 加州 CA001 2020

我需要使用 python pandas 将上述数据转换为以下 json 格式。我正在尝试 df.to_json(orient = 'index') 但它没有创建如下嵌套的甲酸盐。有什么建议么 ?

{
results: [
    {
        account:100,
        Name: Sam,
        LoanDetails : [
            {
                Address1: Street 5,
                State : NY,
                ZIP: NY0001,
                LoanList : [
                    {
                        Loantype: E,
                        expiry: 2012
                    }
                    {
                        Loantype: T,
                        expiry: 2020
                    }
                ]
            }
            {
                Address1: Street 10,
                State: NJ,
                ZIP: Nj0001,
                LoanList: [
                    {
                        Loantype: E,
                        expiry: 2019
                    }
                    {
                        Loantype: T,
                        expiry: 2020
                    }
                ]
            }
    }
    {
        account:100,
        Name: John,
        LoanDetails : 
            {
                Address1: Street 1,
                State : CA,
                ZIP: CA0001,
                LoanList : [
                    {
                        Loantype: E,
                        expiry: 2012
                    }
                    {
                        Loantype: T,
                        expiry: 2020
                    }
                ]
            }
    }
]
}
4

2 回答 2

1

我在下面尝试过并且有效:

import pandas as pd
import json
df = pd.DataFrame({'account':['100','100','100','100','101'],
                    'name':['sam','sam','sam','sam','john'],
                    'address1':['street 5','street 5','street 10','street 10','street 1'],
                    'state':['ny','ny','nj','nj','ca'],
                    'zip':['ny0001','ny0001','nj0001','nj0001','CA001'],
                    'loantype':['e','t','e','t','e'],
                   'expiry':[2019,2020,2019,2020,2019]
                   })


k = df.groupby(['account','name','address1','state']).apply(lambda x:x[['loantype','expiry']].to_dict('records')).reset_index().rename(columns={0:'Loanlist'})#.to_json(orient = 'records')

j = k.groupby(['account','name',]).apply(lambda x:x[['address1','state','Loanlist']].to_dict('records')).reset_index().rename(columns={0:'Loandetails'}).to_json(orient = 'records')

print(j)
于 2021-11-18T18:14:44.157 回答
0

这只是有助于获得你的输出。可能有替代方法。

a = {}
for i in df.index:
    a[i]={}
    a[i]["LoanDetails"] = {}
    a[i]["LoanList"] = {}
    for col in df:
        if col in (["Account","Name"]):
            a[i][col] = df[col].iloc[i]
        if col in (["Address1","State","Zip"]):
            a[i]["LoanDetails"][col] = df[col].iloc[i]
        if col in (["Loantype","expiry"]):
            a[i]["LoanList"][col] = df[col].iloc[i]
b ={}
b["Result"] =[]
for i,v in a.items():
    b["Result"].append(v)
于 2021-11-18T04:02:39.020 回答