多索引Pandas DataFram的嵌套字典

2024-06-26 13:28:21 发布

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

对于嵌套字典,我已尝试了所有可能的解决方案,但无法使任何东西正常工作,因为我的字典是列表和字典的组合:

我从Oandapyv20得到这个结果:

{
    "positions": [
      {
        "instrument": "USD_TRY",
        "long": {
          "units": "19028",
          "averagePrice": "3.96627",
          "pl": "2619.1369",
          "resettablePL": "2619.1369",
          "financing": "-212.5055",
          "guaranteedExecutionFees": "0.0000",
          "tradeIDs": [
            "173664",
            "173783",
            "173785",
            "173787",
            "176966",
          ],
          "unrealizedPL": "-267.6793"
        },
        "short": {
          "units": "0",
          "pl": "0.0000",
          "resettablePL": "0.0000",
          "financing": "0.0000",
          "guaranteedExecutionFees": "0.0000",
          "unrealizedPL": "0.0000"
        },
        "pl": "2619.1369",
        "resettablePL": "2619.1369",
        "financing": "-212.5055",
        "commission": "0.0000",
        "guaranteedExecutionFees": "0.0000",
        "unrealizedPL": "-267.6793",
        "marginUsed": "951.4000"
      },
      {
        "instrument": "USD_MXN",
        "long": {
          "units": "7750",
          "averagePrice": "19.37866",
          "pl": "122.5599",
          "resettablePL": "122.5599",
          "financing": "-48.8715",
          "guaranteedExecutionFees": "0.0000",
          "tradeIDs": [
            "212492",
            "212494",
            "212496",
          ],
          "unrealizedPL": "-41.5788"
        },
        "short": {
          "units": "0",
          "pl": "0.0000",
          "resettablePL": "0.0000",
          "financing": "0.0000",
          "guaranteedExecutionFees": "0.0000",
          "unrealizedPL": "0.0000"
        },
        "pl": "122.5599",
        "resettablePL": "122.5599",
        "financing": "-48.8715",
        "commission": "0.0000",
        "guaranteedExecutionFees": "0.0000",
        "unrealizedPL": "-41.5788",
        "marginUsed": "387.5000"
      },
      {
        "instrument": "USD_NOK",
        "long": {
          "units": "0",
          "pl": "0.0000",
          "resettablePL": "0.0000",
          "financing": "0.0000",
          "guaranteedExecutionFees": "0.0000",
          "unrealizedPL": "0.0000"
        },
        "short": {
          "units": "-13200",
          "averagePrice": "7.65519",
          "pl": "4906.3941",
          "resettablePL": "4906.3941",
          "financing": "-90.9699",
          "guaranteedExecutionFees": "0.0000",
          "tradeIDs": [
            "214255",
            "214257",
            "214259",
            "214281"
          ],
          "unrealizedPL": "-390.0560"
        },
        "pl": "4906.3941",
        "resettablePL": "4906.3941",
        "financing": "-90.9699",
        "commission": "0.0000",
        "guaranteedExecutionFees": "0.0000",
        "unrealizedPL": "-390.0560",
        "marginUsed": "132.0000"
      }
    ],
    "lastTransactionID": "228573"
  }
}

如何将其转换为熊猫数据帧?在

例如,这将给出一个错误:

^{pr2}$

还有这个:

reform = {(level1_key, level2_key, level3_key): values
    for level1_key, level2_dict in x.items()
    for level2_key, level3_dict in level2_dict.items()
    for level3_key, values      in level3_dict.items()}

AttributeError: 'list' object has no attribute 'items'

是否可以将上述内容输入到数据帧中,而不必进行绝望的尝试,包括for循环和try&except?在

提前谢谢


Tags: keyfor字典itemsdictusdplunits