【问题标题】:reshape nested json data in a dataframe using python to get desired output使用python重塑数据框中的嵌套json数据以获得所需的输出
【发布时间】:2022-08-10 22:27:01
【问题描述】:

您好我正在尝试使用熊猫在数据框中重塑此 json 数据。

      id        categories
1     3ee877e0  [{\"entity_def_id\":\"category\",\"permalink\":\"blockchain\",\"uuid\":\"1fea6201\",\"value\":\"Blockchain\"},{\"entity_def_id\":\"category\",\"permalink\":\"cryptocurrency\",\"uuid\":\"bd082f4d\",\"value\":\"Cryptocurrency\"},{\"entity_def_id\":\"category\",\"permalink\":\"loyalty-programs\",\"uuid\":\"4a45af54\",\"value\":\"Loyalty Programs\"},{\"entity_def_id\":\"category\",\"permalink\":\"marketplace-772d\",\"uuid\":\"772da8fe\",\"value\":\"Marketplace\"},{\"entity_def_id\":\"category\",\"permalink\":\"software\",\"uuid\":\"c08b5441\",\"value\":\"Software\"}]

预期结果

id        entity_def_id  permalink         uuid        value
3ee877e0  category       blockchain        1fea6201    Blockchain
3ee877e0  category       cryptocurrency    bd082f4d    Cryptocurrency
3ee877e0  category       loyalty-programs  4a45af54    Loyalty Programs
3ee877e0  category       marketplace-772d  772da8fe    Marketplace
3ee877e0  category       software          c08b5441    Software

很抱歉没有发布我这样做的尝试,但我是 python 新手,并且已经知道如何在 mongodb 和 dataiku 中执行此操作,只是想知道使用 python 执行此操作的方法。

    标签: python pandas dataframe dataiku


    【解决方案1】:

    您可以尝试explodecategories 列,然后将categories 列中的字典转换为多列

    out = (df.explode('categories', ignore_index=True)
           .pipe(lambda df: df.join(pd.DataFrame(df.pop('categories').values.tolist()))))
    
    print(out)
    
             id entity_def_id         permalink      uuid             value
    0  3ee877e0      category        blockchain  1fea6201        Blockchain
    1  3ee877e0      category    cryptocurrency  bd082f4d    Cryptocurrency
    2  3ee877e0      category  loyalty-programs  4a45af54  Loyalty Programs
    3  3ee877e0      category  marketplace-772d  772da8fe       Marketplace
    4  3ee877e0      category          software  c08b5441          Software
    

    【讨论】:

      【解决方案2】:

      您可以获取categories 中的字典列表,按原样将其传递给DataFrame(),然后使用insert 插入您的id 列

      import pandas as pd
      
      current_df = pd.DataFrame({"id": "3ee877e0","categories":[[{"entity_def_id":"category","permalink":"blockchain","uuid":"1fea6201","value":"Blockchain"},{"entity_def_id":"category","permalink":"cryptocurrency","uuid":"bd082f4d","value":"Cryptocurrency"},{"entity_def_id":"category","permalink":"loyalty-programs","uuid":"4a45af54","value":"Loyalty Programs"},{"entity_def_id":"category","permalink":"marketplace-772d","uuid":"772da8fe","value":"Marketplace"},{"entity_def_id":"category","permalink":"software","uuid":"c08b5441","value":"Software"}]]})
      
      id_ = current_df.loc[:,"id"].values[0]
      categories = current_df.loc[:,"categories"].values[0]
      
      new_df = pd.DataFrame(categories )
      new_df.insert(0,"id", id_ )
      

      结果

               id entity_def_id         permalink      uuid             value
      0  3ee877e0      category        blockchain  1fea6201        Blockchain
      1  3ee877e0      category    cryptocurrency  bd082f4d    Cryptocurrency
      2  3ee877e0      category  loyalty-programs  4a45af54  Loyalty Programs
      3  3ee877e0      category  marketplace-772d  772da8fe       Marketplace
      4  3ee877e0      category          software  c08b5441          Software
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-28
        • 1970-01-01
        • 2023-02-09
        • 2022-01-16
        • 2021-07-14
        • 2020-02-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多