将 JSON 结构重新排列为 Python 中的数据框
Rearrange JSON structure to a dataframe in Python
我下载的表格中有下面列出的 json 结构。我想把它放在像这种旋转形式的数据帧结构中。
日期账户金额
2019-12-31资本盈余22165000000
2019-12-31总Liab 225307000000
2019-12-31 总股东权益 33185000000
2019-12-31 minorityInterest 45000000
2019-12-31 otherCurrentLiab 21454000000
2019-12-31总资产258537000000
2019-12-31 普通股 41000000
.
.
.
2018-12-31 无形资产 178000000
capitalSurplus 22006000000
totalLiab 220474000000
totalStockholderEquity 35932000000
{'balanceSheetHistory': {'F': [{'2019-12-31': {'capitalSurplus': 22165000000,
'totalLiab': 225307000000,
'totalStockholderEquity': 33185000000,
'minorityInterest': 45000000,
'otherCurrentLiab': 21454000000,
'totalAssets': 258537000000,
'commonStock': 41000000,
'otherCurrentAssets': 2699000000,
'retainedEarnings': 20320000000,
'otherLiab': 23723000000,
'treasuryStock':-9341000000,
'otherAssets': 20599000000,
'cash': 8437000000,
'totalCurrentLiabilities': 98132000000,
'deferredLongTermAssetCharges': 11863000000,
'shortLongTermDebt': 1168000000,
'otherStockholderEquity':-7728000000,
'propertyPlantEquipment': 37869000000,
'totalCurrentAssets': 114047000000,
'longTermInvestments': 2396000000,
'netTangibleAssets': 33185000000,
'shortTermInvestments': 13851000000,
'netReceivables': 3618000000,
'longTermDebt': 13618000000,
'inventory': 10786000000,
'accountsPayable': 19681000000}},
{'2018-12-31': {'intangibleAssets': 178000000,
'capitalSurplus': 22006000000,
'totalLiab': 220474000000,
'totalStockholderEquity': 35932000000,
'minorityInterest': 134000000,
'deferredLongTermLiab': 247000000,
'otherCurrentLiab': 17270000000,
'totalAssets': 256540000000,
'commonStock': 41000000,
'otherCurrentAssets': 3930000000,
'retainedEarnings': 22668000000,
'otherLiab': 24185000000,
'goodWill': 264000000,
'treasuryStock':-8783000000,
'otherAssets': 17245000000,
'cash': 7111000000,
'totalCurrentLiabilities': 95569000000,
'deferredLongTermAssetCharges': 10412000000,
'shortLongTermDebt': 1700000000,
'otherStockholderEquity':-7366000000,
'propertyPlantEquipment': 37883000000,
'totalCurrentAssets': 114649000000,
'longTermInvestments': 2959000000,
'netTangibleAssets': 35490000000,
'shortTermInvestments': 15925000000,
'netReceivables': 11195000000,
'longTermDebt': 11833000000,
'inventory': 11220000000,
'accountsPayable': 21520000000}},
{'2017-12-31': {'intangibleAssets': 213000000,
'capitalSurplus': 21843000000,
'totalLiab': 222792000000,
'totalStockholderEquity': 35578000000,
'minorityInterest': 126000000,
'deferredLongTermLiab': 232000000,
'otherCurrentLiab': 16402000000,
'totalAssets': 258496000000,
'commonStock': 41000000,
'otherCurrentAssets': 3649000000,
'retainedEarnings': 21906000000,
'otherLiab': 25526000000,
'goodWill': 75000000,
'treasuryStock':-8212000000,
'otherAssets': 18091000000,
'cash': 8934000000,
'totalCurrentLiabilities': 94600000000,
'deferredLongTermAssetCharges': 10762000000,
'shortLongTermDebt': 1960000000,
'otherStockholderEquity':-6959000000,
'propertyPlantEquipment': 36901000000,
'totalCurrentAssets': 116801000000,
'longTermInvestments': 3448000000,
'netTangibleAssets': 35290000000,
'shortTermInvestments': 17554000000,
'netReceivables': 10599000000,
'longTermDebt': 13174000000,
'inventory': 11176000000,
'accountsPayable': 23282000000}},
{'2016-12-31': {'intangibleAssets': 198000000,
'capitalSurplus': 21630000000,
'totalLiab': 208668000000,
'totalStockholderEquity': 29170000000,
'minorityInterest': 113000000,
'otherCurrentLiab': 16277000000,
'totalAssets': 237951000000,
'commonStock': 41000000,
'otherCurrentAssets': 3145000000,
'retainedEarnings': 15634000000,
'otherLiab': 25086000000,
'goodWill': 50000000,
'treasuryStock':-8135000000,
'otherAssets': 14894000000,
'cash': 7828000000,
'totalCurrentLiabilities': 90281000000,
'deferredLongTermAssetCharges': 9705000000,
'shortLongTermDebt': 1361000000,
'otherStockholderEquity':-7013000000,
'propertyPlantEquipment': 33692000000,
'totalCurrentAssets': 108461000000,
'longTermInvestments': 3523000000,
'netTangibleAssets': 28922000000,
'shortTermInvestments': 19642000000,
'netReceivables': 11102000000,
'longTermDebt': 13222000000,
'inventory': 8898000000,
'accountsPayable': 21296000000}}]}}
试试这个代码
cols = ['date','text','valeus']
dat = pd.DataFrame(columns = cols)
dt=#idid copy past the json file
dic=dt['balanceSheetHistory']['F']
for index in range(len(dic)):
for k in list(dic[index].keys()):
for k2 in list(dic[index][k].keys()):
k3=dic[index][k][k2]
#dflist.append([k,k2,dic[index][k][k2]])
dat = dat.append({'date':k, 'text':k2,'valeus':k3},ignore_index=True)
我下载的表格中有下面列出的 json 结构。我想把它放在像这种旋转形式的数据帧结构中。
日期账户金额
2019-12-31资本盈余22165000000
2019-12-31总Liab 225307000000
2019-12-31 总股东权益 33185000000
2019-12-31 minorityInterest 45000000
2019-12-31 otherCurrentLiab 21454000000
2019-12-31总资产258537000000
2019-12-31 普通股 41000000 . . . 2018-12-31 无形资产 178000000
capitalSurplus 22006000000
totalLiab 220474000000
totalStockholderEquity 35932000000
{'balanceSheetHistory': {'F': [{'2019-12-31': {'capitalSurplus': 22165000000, 'totalLiab': 225307000000, 'totalStockholderEquity': 33185000000, 'minorityInterest': 45000000, 'otherCurrentLiab': 21454000000, 'totalAssets': 258537000000, 'commonStock': 41000000, 'otherCurrentAssets': 2699000000, 'retainedEarnings': 20320000000, 'otherLiab': 23723000000, 'treasuryStock':-9341000000, 'otherAssets': 20599000000, 'cash': 8437000000, 'totalCurrentLiabilities': 98132000000, 'deferredLongTermAssetCharges': 11863000000, 'shortLongTermDebt': 1168000000, 'otherStockholderEquity':-7728000000, 'propertyPlantEquipment': 37869000000, 'totalCurrentAssets': 114047000000, 'longTermInvestments': 2396000000, 'netTangibleAssets': 33185000000, 'shortTermInvestments': 13851000000, 'netReceivables': 3618000000, 'longTermDebt': 13618000000, 'inventory': 10786000000, 'accountsPayable': 19681000000}}, {'2018-12-31': {'intangibleAssets': 178000000, 'capitalSurplus': 22006000000, 'totalLiab': 220474000000, 'totalStockholderEquity': 35932000000, 'minorityInterest': 134000000, 'deferredLongTermLiab': 247000000, 'otherCurrentLiab': 17270000000, 'totalAssets': 256540000000, 'commonStock': 41000000, 'otherCurrentAssets': 3930000000, 'retainedEarnings': 22668000000, 'otherLiab': 24185000000, 'goodWill': 264000000, 'treasuryStock':-8783000000, 'otherAssets': 17245000000, 'cash': 7111000000, 'totalCurrentLiabilities': 95569000000, 'deferredLongTermAssetCharges': 10412000000, 'shortLongTermDebt': 1700000000, 'otherStockholderEquity':-7366000000, 'propertyPlantEquipment': 37883000000, 'totalCurrentAssets': 114649000000, 'longTermInvestments': 2959000000, 'netTangibleAssets': 35490000000, 'shortTermInvestments': 15925000000, 'netReceivables': 11195000000, 'longTermDebt': 11833000000, 'inventory': 11220000000, 'accountsPayable': 21520000000}}, {'2017-12-31': {'intangibleAssets': 213000000, 'capitalSurplus': 21843000000, 'totalLiab': 222792000000, 'totalStockholderEquity': 35578000000, 'minorityInterest': 126000000, 'deferredLongTermLiab': 232000000, 'otherCurrentLiab': 16402000000, 'totalAssets': 258496000000, 'commonStock': 41000000, 'otherCurrentAssets': 3649000000, 'retainedEarnings': 21906000000, 'otherLiab': 25526000000, 'goodWill': 75000000, 'treasuryStock':-8212000000, 'otherAssets': 18091000000, 'cash': 8934000000, 'totalCurrentLiabilities': 94600000000, 'deferredLongTermAssetCharges': 10762000000, 'shortLongTermDebt': 1960000000, 'otherStockholderEquity':-6959000000, 'propertyPlantEquipment': 36901000000, 'totalCurrentAssets': 116801000000, 'longTermInvestments': 3448000000, 'netTangibleAssets': 35290000000, 'shortTermInvestments': 17554000000, 'netReceivables': 10599000000, 'longTermDebt': 13174000000, 'inventory': 11176000000, 'accountsPayable': 23282000000}}, {'2016-12-31': {'intangibleAssets': 198000000, 'capitalSurplus': 21630000000, 'totalLiab': 208668000000, 'totalStockholderEquity': 29170000000, 'minorityInterest': 113000000, 'otherCurrentLiab': 16277000000, 'totalAssets': 237951000000, 'commonStock': 41000000, 'otherCurrentAssets': 3145000000, 'retainedEarnings': 15634000000, 'otherLiab': 25086000000, 'goodWill': 50000000, 'treasuryStock':-8135000000, 'otherAssets': 14894000000, 'cash': 7828000000, 'totalCurrentLiabilities': 90281000000, 'deferredLongTermAssetCharges': 9705000000, 'shortLongTermDebt': 1361000000, 'otherStockholderEquity':-7013000000, 'propertyPlantEquipment': 33692000000, 'totalCurrentAssets': 108461000000, 'longTermInvestments': 3523000000, 'netTangibleAssets': 28922000000, 'shortTermInvestments': 19642000000, 'netReceivables': 11102000000, 'longTermDebt': 13222000000, 'inventory': 8898000000, 'accountsPayable': 21296000000}}]}}
试试这个代码
cols = ['date','text','valeus']
dat = pd.DataFrame(columns = cols)
dt=#idid copy past the json file
dic=dt['balanceSheetHistory']['F']
for index in range(len(dic)):
for k in list(dic[index].keys()):
for k2 in list(dic[index][k].keys()):
k3=dic[index][k][k2]
#dflist.append([k,k2,dic[index][k][k2]])
dat = dat.append({'date':k, 'text':k2,'valeus':k3},ignore_index=True)