首页 > 解决方案 > json 在导入 json 时将 json 标头附加到名称中?

问题描述

我正在尝试将嵌套的 json 文件导入数据框。我想要这种格式:

year   columnA   columnB   ...
2018   ABC       123

但我的 Json 采用这种格式:

{'2000-09-30': {'accountsPayable': '1157000000.00',
  'accumulatedAmortization': None,
  'accumulatedDepreciation': None,
  'accumulatedOtherComprehensiveIncome': None,
  'additionalPaidInCapital': None,
  'capitalLeaseObligations': None,
  'capitalSurpluse': None,
  'cash': '1191000000.00',
  'cashAndShortTermInvestments': None,
  'commonStock': '1502000000.00',
  'commonStockSharesOutstanding': None,
  'commonStockTotalEquity': None,
  'currency_symbol': 'USD',
  'date': '2000-09-30',
  'deferredLongTermAssetCharges': None,
  'deferredLongTermLiab': None,
  'earningAssets': None,
  'filing_date': None,
  'goodWill': None,
  'intangibleAssets': None,
  'inventory': '33000000.00',
  'liabilitiesAndStockholdersEquity': None,
  'longTermDebt': '300000000.00',
  'longTermDebtTotal': None,
  'longTermInvestments': None,
  'negativeGoodwill': None,
  'netReceivables': None,
  'netTangibleAssets': None,
  'nonCurrentAssetsTotal': None,
  'nonCurrentLiabilitiesOther': None,
  'nonCurrentLiabilitiesTotal': None,
  'nonCurrrentAssetsOther': None,
  'noncontrollingInterestInConsolidatedEntity': None,
  'otherAssets': '1063000000.00',
  'otherCurrentAssets': '414000000.00',
  'otherCurrentLiab': '776000000.00',
  'otherLiab': None,
  'otherStockholderEquity': None,
  'preferredStockRedeemable': None,
  'preferredStockTotalEquity': '76000000.00',
  'propertyPlantAndEquipmentGross': None,
  'propertyPlantEquipment': '313000000.00',
  'retainedEarnings': '2529000000.00',
  'retainedEarningsTotalEquity': None,
  'shortLongTermDebt': None,
  'shortLongTermDebtTotal': None,
  'shortTermDebt': None,
  'shortTermInvestments': '2836000000.00',
  'temporaryEquityRedeemableNoncontrollingInterests': None,
  'totalAssets': '6803000000.00',
  'totalCurrentAssets': None,
  'totalCurrentLiabilities': '1933000000.00',
  'totalLiab': '2696000000.00',
  'totalPermanentEquity': None,
  'totalStockholderEquity': '4107000000.00',
  'treasuryStock': None,
  'warrants': None},
 '2001-09-30': {'accountsPayable': '801000000.00',
  'accumulatedAmortization': None,
  'accumulatedDepreciation': None,
  'accumulatedOtherComprehensiveIncome': None,
  'additionalPaidInCapital': None,
  'capitalLeaseObligations': None,
  'capitalSurpluse': None,
  'cash': '2310000000.00',
  'cashAndShortTermInvestments': None,
  'commonStock': '1693000000.00',
  'commonStockSharesOutstanding': None,
  'commonStockTotalEquity': None,
  'currency_symbol': 'USD',
  'date': '2001-09-30',
  'deferredLongTermAssetCharges': None,
  'deferredLongTermLiab': None,
  'earningAssets': None,
  'filing_date': None,
  'goodWill': None,
  'intangibleAssets': None,
  'inventory': '11000000.00',
  'liabilitiesAndStockholdersEquity': None,
  'longTermDebt': '317000000.00',
  'longTermDebtTotal': None,
  'longTermInvestments': None,
  'negativeGoodwill': None,
  'netReceivables': None,
  'netTangibleAssets': None,
  'nonCurrentAssetsTotal': None,
  'nonCurrentLiabilitiesOther': None,
  'nonCurrentLiabilitiesTotal': None,
  'nonCurrrentAssetsOther': None,
  'noncontrollingInterestInConsolidatedEntity': None,
  'otherAssets': '314000000.00',
  'otherCurrentAssets': '330000000.00',
  'otherCurrentLiab': '717000000.00',
  'otherLiab': None,
  'otherStockholderEquity': None,
  'preferredStockRedeemable': None,
  'preferredStockTotalEquity': None,
  'propertyPlantAndEquipmentGross': None,
  'propertyPlantEquipment': '564000000.00',
  'retainedEarnings': '2238000000.00',
  'retainedEarningsTotalEquity': None,
  'shortLongTermDebt': None,
  'shortLongTermDebtTotal': None,
  'shortTermDebt': None,
  'shortTermInvestments': '2026000000.00',
  'temporaryEquityRedeemableNoncontrollingInterests': None,
  'totalAssets': '6021000000.00',
  'totalCurrentAssets': None,
  'totalCurrentLiabilities': '1518000000.00',
  'totalLiab': '2101000000.00',
  'totalPermanentEquity': None,
  'totalStockholderEquity': '3920000000.00',
  'treasuryStock': None,
  'warrants': None}

这是我的代码(它应该可以工作,它使用的是网站的示例令牌):

import json 
import requests
import pandas as pd

companyData = requests.get(url="https://eodhistoricaldata.com/api/fundamentals/AAPL.US?api_token=OeAFFmMliFG5orCUuwAKQ8l4WWFQ67YX").json()
df = pd.json_normalize(companyData['Financials']['Balance_Sheet']['yearly'])
df

结果是:

2020-09-30.date 2020-09-30.filing_date  2020-09-30.currency_symbol  2020-09-30.totalAssets  2020-09-30.intangibleAssets 2020-09-30.earningAssets    2020-09-30.otherCurrentAssets   2020-09-30.totalLiab    2020-09-30.totalStockholderEquity   2020-09-30.deferredLongTermLiab 2020-09-30.otherCurrentLiab 2020-09-30.commonStock  2020-09-30.retainedEarnings 2020-09-30.otherLiab    2020-09-30.goodWill 2020-09-30.otherAssets  2020-09-30.cash 2020-09-30.totalCurrentLiabilities  2020-09-30.shortTermDebt    2020-09-30.shortLongTermDebt    2020-09-30.shortLongTermDebtTotal   2020-09-30.otherStockholderEquity   2020-09-30.propertyPlantEquipment   2020-09-30.totalCurrentAssets   2020-09-30.longTermInvestments  2020-09-30.netTangibleAssets    2020-09-30.shortTermInvestments 2020-09-30.netReceivables   2020-09-30.longTermDebt 2020-09-30.inventory    2020-09-30.accountsPayable  2020-09-30.totalPermanentEquity 2020-09-30.noncontrollingInterestInConsolidatedEntity   2020-09-30.temporaryEquityRedeemableNoncontrollingInterests 2020-09-30.accumulatedOtherComprehensiveIncome  2020-09-30.additionalPaidInCapital  2020-09-30.commonStockTotalEquity   2020-09-30.preferredStockTotalEquity    2020-09-30.retainedEarningsTotalEquity  2020-09-30.treasuryStock    ... 2000-09-30.totalCurrentLiabilities  2000-09-30.shortTermDebt    2000-09-30.shortLongTermDebt    2000-09-30.shortLongTermDebtTotal   2000-09-30.otherStockholderEquity   2000-09-30.propertyPlantEquipment   2000-09-30.totalCurrentAssets   2000-09-30.longTermInvestments  2000-09-30.netTangibleAssets    2000-09-30.shortTermInvestments 2000-09-30.netReceivables   2000-09-30.longTermDebt 2000-09-30.inventory    2000-09-30.accountsPayable  2000-09-30.totalPermanentEquity 2000-09-30.noncontrollingInterestInConsolidatedEntity   2000-09-30.temporaryEquityRedeemableNoncontrollingInterests 2000-09-30.accumulatedOtherComprehensiveIncome  2000-09-30.additionalPaidInCapital  2000-09-30.commonStockTotalEquity   2000-09-30.preferredStockTotalEquity    2000-09-30.retainedEarningsTotalEquity  2000-09-30.treasuryStock    2000-09-30.accumulatedAmortization  2000-09-30.nonCurrrentAssetsOther   2000-09-30.deferredLongTermAssetCharges 2000-09-30.nonCurrentAssetsTotal    2000-09-30.capitalLeaseObligations  2000-09-30.longTermDebtTotal    2000-09-30.nonCurrentLiabilitiesOther   2000-09-30.nonCurrentLiabilitiesTotal   2000-09-30.negativeGoodwill 2000-09-30.warrants 2000-09-30.preferredStockRedeemable 2000-09-30.capitalSurpluse  2000-09-30.liabilitiesAndStockholdersEquity 2000-09-30.cashAndShortTermInvestments  2000-09-30.propertyPlantAndEquipmentGross   2000-09-30.accumulatedDepreciation  2000-09-30.commonStockSharesOutstanding
0   2020-09-30  2020-10-30  USD 323888000000.00 None    None    11264000000.00  258549000000.00 65339000000.00  None    49327000000.00  50779000000.00  14966000000.00  46108000000.00  None    33952000000.00  38016000000.00  105392000000.00 13769000000.00  13769000000.00  None    -406000000.00   36766000000.00  143713000000.00 100887000000.00 65339000000.00  52927000000.00  37445000000.00  98667000000.00  4061000000.00   42296000000.00  None    None    None    None    None    50779000000.00  None    14966000000.00  None    ... 1933000000.00   None    None    None    None    313000000.00    None    None    None    2836000000.00   None    300000000.00    33000000.00 1157000000.00   None    None    None    None    None    None    76000000.00 None    None    None    None    None    None    None    None    None    None    None    None    None    None    None    None    None    None    None
1 rows × 1197 columns

我的目标是把它变成一种格式,它是数据集的相关列,然后 year 是一行,其中包含该 json 中键中的所有值,就像我上面的示例一样。

我能做些什么?

标签: pythonpandas

解决方案


推荐阅读