将Python JSON转换为数据帧

2024-05-13 01:29:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Yahoo finance Python库获取会计财务数据,以进行一些基本分析。所有财务报表数据均采用JSON格式。我希望数据采用表格格式,就像我通常在Python数据帧中看到的那样。您好,数据周围有几个包装器,我不确定如何删除它们,以便将数据放入一个简单的列和行数据框中。下面是Python的外观:

{
   "incomeStatementHistory":{
      "F":[
         {
            "2019-12-31":{
               "researchDevelopment":"None",
               "effectOfAccountingCharges":"None",
               "incomeBeforeTax":-640000000,
               "minorityInterest":45000000,
               "netIncome":47000000,
               "sellingGeneralAdministrative":10218000000,
               "grossProfit":12876000000,
               "ebit":2658000000,
               "operatingIncome":2658000000,
               "otherOperatingExpenses":"None",
               "interestExpense":-1049000000,
               "extraordinaryItems":"None",

Tags: 数据nonejson格式yahoo表格外观finance
2条回答

你应该用熊猫 Here its a tutorial of how to do that with pandas

你也可以检查这个question

你没有完整的回答,所以很难判断这是否是你想要的

d = {
   "incomeStatementHistory":{
      "F":[
         {
            "2019-12-31":{
               "researchDevelopment":"None",
               "effectOfAccountingCharges":"None",
               "incomeBeforeTax":-640000000,
               "minorityInterest":45000000,
               "netIncome":47000000,
               "sellingGeneralAdministrative":10218000000,
               "grossProfit":12876000000,
               "ebit":2658000000,
               "operatingIncome":2658000000,
               "otherOperatingExpenses":"None",
               "interestExpense":-1049000000,
               "extraordinaryItems":"None",}}]}}


pd.json_normalize(d['incomeStatementHistory']['F'])

输出:

  2019-12-31.researchDevelopment 2019-12-31.effectOfAccountingCharges  2019-12-31.incomeBeforeTax  ...  2019-12-31.otherOperatingExpenses  2019-12-31.interestExpense  2019-12-31.extraordinaryItems
0                           None                                 None                  -640000000  ...                               None                 -1049000000                           None

[1 rows x 12 columns]

相关问题 更多 >