我有嵌套字典格式的資料。我需要檢索帳戶鍵中的資料,該鍵本身具有重復的字典鍵和值的串列。我有嵌套陣列以相同但不同的值重復 28 次。
[{"report": {
"accounts": [
{
"balance": "0",
"balancedate": "2021-08-28T00:00:00 00:00",
"balloonpayment": null,
"businesstype": "Bank Credit Cards",
"businesstype_raw": "BC",
"classification": "REVOLVING",
"classification_raw": null,
"companysoldto": null,
"creditor": {
"addrln1": "3311 MILL MEADOW DR",
"addrln2": null,
"city": "HILLIARD",
"phone": "BYMAILONLY",
"state": "OH",
"subcode": "2250030",
"zip": "43026"
},
"creditorcomments": [
{
"commenttext": "Account closed at credit grantor’s request",
"comment_raw": "18"
}
],
"creditorcommentsraw": null,
"dateopened": "2018-04-21T00:00:00 00:00",
"delinquent30dayscount": 0,
"delinquent60dayscount": 0,
"delinquent90plusdayscount": 0,
"highbalance": null,
"limit": null,
"monthlypayment": null,
"name": "DISCOVER FINANCIAL SVC",
"number": null,
"openclosed": "Closed",
"originalamount": null,
"originalcreditor": null,
"pastdueamount": ""},
{
"balance": "",
"balancedate": "2021-07-28T00:00:00 00:00",
"balloonpayment": null,
"businesstype": "Bank Credit Cards",
"businesstype_raw": "BC",
"classification": "REVOLVING",
"classification_raw": null,
"companysoldto": null,
"creditor": {
"addrln1": "PO BOX 15369",
"addrln2": null,
"city": "WILMINGTON",
"phone": "8009452000",
"state": "DE",
"subcode": "1200320",
"zip": "19850"
},
"creditorcomments": [
{
"commenttext": "Account closed at credit grantor’s request",
"comment_raw": "18"
}
],
"creditorcommentsraw": null,
"dateopened": "2020-01-21T00:00:00 00:00",
"delinquent30dayscount": 0,
"delinquent60dayscount": 0,
"delinquent90plusdayscount": 0,
"highbalance": "40",
"limit": "11000",
"monthlypayment": null,
"name": "BANK CREDIT CARD",
"number": null,
"openclosed": "Closed",
"originalamount": null,
"originalcreditor": null,
"pastdueamount": "",
"paymenthistories": [
{
最終輸出字典應如下所示:
{'balance': ["0","",""] #all the data in the nested dict
'balancedata': ["2021-08-28T00:00:00 00:00",""2021-07-28T00:00:00 00:00""],
'balloonpayment':[" "]
so on for all the keys
uj5u.com熱心網友回復:
如果我對您的理解正確,那么您實際上是在轉置資料矩陣。如果沒有針對它的三行 pandas 資料框解決方案,我會感到非常驚訝,但純 python 也可以。
iv = [ {'a':0, 'b':10},{'a':1, 'b':11}]
rv = dict()
for k in iv[0]:
rv[k]=[i[k] for i in iv]
print( rv)
這是假設所有條目都包含所有鍵。如果這不成立,您需要添加一些默認值并在第一遍中收集所有鍵的集合
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/447817.html
