python - 在带有数组的嵌套 Json 上使用 Pandas json_normalize

标签 python json pandas normalize

问题是用嵌套的 json 对象数组规范化 json。我看过类似的问题并尝试使用他们的解决方案无济于事。

这就是我的 json 对象的样子。

{
  "results": [
    {
      "_id": "25",
      "Product": {
        "Description": "3 YEAR",
        "TypeLevel1": "INTEREST",
        "TypeLevel2": "LONG"
      },
      "Settlement": {},
      "Xref": {
        "SCSP": "96"
      },
      "ProductSMCP": [
        {
          "SMCP": "01"
        }
      ]
    },
    {
      "_id": "26",
      "Product": {
        "Description": "10 YEAR",
        "TypeLevel1": "INTEREST",
        "Currency": "USD",
        "Operational": true,
        "TypeLevel2": "LONG"
      },
      "Settlement": {},
      "Xref": {
        "BBT": "CITITYM9",
        "TCK": "ZN"
      },
      "ProductSMCP": [
        {
          "SMCP": "01"
        },
        {
          "SMCP2": "02"
        }
      ]
    }
  ]
}

这是我规范化 json 对象的代码。

data = json.load(j)
data = data['results']
print pd.io.json.json_normalize(data)

想要的结果应该是这样的

id   Description    TypeLevel1   TypeLevel2  Currency  \
25   3 YEAR US      INTEREST     LONG        NAN
26   10 YEAR US     INTEREST     NAN         USD

BBT   TCT  SMCP  SMCP2  SCSP   
NAN   NAN  521   NAN    01
M9    ZN   01    02     NAN

然而,我得到的结果是这样的:

  Product.Currency Product.Description Product.Operational Product.TypeLevel1  \
0              NaN              3 YEAR                 NaN           INTEREST
1              USD             10 YEAR                True           INTEREST

  Product.TypeLevel2                        ProductSMCP  Xref.BBT Xref.SCSP  \
0               LONG                   [{'SMCP': '01'}]       NaN        96
1               LONG  [{'SMCP': '01'}, {'SMCP2': '02'}]  CITITYM9       NaN

  Xref.TCK _id
0      NaN  25
1       ZN  26

如您所见,问题出在 ProductSCMP它并没有完全展平阵列。

最佳答案

一旦我们通过了第一次规范化,我将应用 lambda 来完成这项工作。

from cytoolz.dicttoolz import merge

pd.io.json.json_normalize(data).pipe(
    lambda x: x.drop('ProductSMCP', 1).join(
        x.ProductSMCP.apply(lambda y: pd.Series(merge(y)))
    )
)

  Product.Currency Product.Description Product.Operational Product.TypeLevel1 Product.TypeLevel2  Xref.BBT Xref.SCSP Xref.TCK _id SMCP SMCP2
0              NaN              3 YEAR                 NaN           INTEREST               LONG       NaN        96      NaN  25   01   NaN
1              USD             10 YEAR                True           INTEREST               LONG  CITITYM9       NaN       ZN  26   01    02

修剪列名

pd.io.json.json_normalize(data).pipe(
    lambda x: x.drop('ProductSMCP', 1).join(
        x.ProductSMCP.apply(lambda y: pd.Series(merge(y)))
    )
).rename(columns=lambda x: re.sub('(Product|Xref)\.', '', x))

  Currency Description Operational TypeLevel1 TypeLevel2       BBT SCSP  TCK _id SMCP SMCP2
0      NaN      3 YEAR         NaN   INTEREST       LONG       NaN   96  NaN  25   01   NaN
1      USD     10 YEAR        True   INTEREST       LONG  CITITYM9  NaN   ZN  26   01    02

关于python - 在带有数组的嵌套 Json 上使用 Pandas json_normalize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45418334/

相关文章:

python - Bokeh - 当 output_backend = "webgl"时,绘图在 x 轴上重叠

python - 二叉树获取每个级别的所有节点

javascript - 无法在 Django 模板 html 脚本中将 python 字典用作 JSON

c# - 使用 C# 从 Google Translation API 反序列化 JSON

javascript - 在禁用 ng 的情况下搜索 JSON 对象(AngularJS)

python - pandas 为每个 DatetimeIndex 条目获取第一个过滤行的有效方法

python - 如何使用 SWIG 在 python 中扩展模板化的 c++ 类以允许 [] 运算符

python - flask 中有没有办法获取 request.json 中的每个对象并将每个属性保存在模型上?

python - 根据 pandas 中的 csv 文件名重命名列

python - Pandas - KeyError : '[] not in index' when training a Keras model