将嵌套的 Json 文件展平为 Pandas 数据框

Posted

技术标签:

【中文标题】将嵌套的 Json 文件展平为 Pandas 数据框【英文标题】:Flattening nested Json file into pandas dataframe 【发布时间】:2021-05-14 08:32:42 【问题描述】:

我有这个 json 文件

    
    "OrderMaster": 
        "Order": 
            "item": [
                "row_id": "1-2LDPVI0",
                "sequence_id": "3851101",
                "end_date": "",
                "name": "TV-Discount",
                "orderable": "Y",
                "period": "",
                "period_uom": "",
                "phone_number_flag": "N",
                "price_type": "Recurring",
                "product_category": "mobilepackage",
                "product_sub_category": "Discount",
                "product_type_code": "Product",
                "type": "PhoneOrder",
                "vendor_part_number": "",
                "created_date": "2018-02-16 09:09:24",
                "created_by": "id123",
                "last_updated_date": "2020-09-14 09:39:24",
                "last_updated_by": "id123",
                "ts_event_notification_time": "2020-09-14 09:40:69",
                "OrderItems": 
                    "item": [
                        "original_list_price": "0",
                        "order_list_id": "1-4ABU",
                        "order_list_name": "SEK Pricelist",
                        "product_id": "1-2LDPUKX",
                        "start_date": "2018-02-17 00:00:00"
                    ,
                    
                        "original_list_price": "45",
                        "order_list_id": "1-4AFU",
                        "order_list_name": "SEK Pricelist",
                        "product_id": "1-2LGSDFUKX",
                        "start_date": "2018-02-18 00:04:20"
                    ]
                
            ,
            
                "row_id": "1-2LDPVI0",
                "sequence_id": "3851101",
                "end_date": "",
                "name": "TV-Discount",
                "orderable": "Y",
                "period": "",
                "period_uom": "",
                "phone_number_flag": "N",
                "price_type": "Recurring",
                "product_category": "mobilepackage",
                "product_sub_category": "Discount",
                "product_type_code": "Product",
                "type": "PhoneOrder",
                "vendor_part_number": "",
                "created_date": "2018-02-16 09:19:24",
                "created_by": "id123",
                "last_updated_date": "2020-09-15 09:39:24",
                "last_updated_by": "id123",
                "ts_event_notification_time": "2020-09-14 09:40:28",
                "OrderItems": 
                    "item": [
                        "original_list_price": "42",
                        "order_list_id": "1-4ABU",
                        "order_list_name": "SEK Pricelist",
                        "product_id": "1-2LDPUKX",
                        "start_date": "2018-02-19 00:00:00"
                    ,
                    
                        "original_list_price": "42",
                        "order_list_id": "1-4ASU",
                        "order_list_name": "SEK Pricelist",
                        "product_id": "1-2LDDAKX",
                        "start_date": "2018-02-12 00:00:00"
                    ,
                    
                        "original_list_price": "43",
                        "order_list_id": "1-4FDBU",
                        "order_list_name": "SEK Pricelist",
                        "product_id": "1-2LDFSDFKX",
                        "start_date": "2018-02-11 00:00:00"
                    ]
                
            ]
        
    

这就是我想要实现的目标:

到目前为止,我已经设法找到了这个point 但是我对最后一个嵌套列“OrderItem”列有问题。我设法extract 它但很难弄清楚如何将它们连接在一起,就像在目标结果中一样。

【问题讨论】:

【参考方案1】:

我设法通过使用带有正确参数集的 json_normalise 解决了这个问题

with open(file_path) as f:
    data = json.load(f)

# Define feature list for dataframe
features = [
    "row_id",
    "sequence_id",
    "end_date",
    "name",
    "orderable",
    "period",
    "period_uom",
    "phone_number_flag",
    "price_type",
    "product_category",
    "product_sub_category",
    "product_type_code",
    "type",
    "vendor_part_number",
    "created_date",
    "created_by",
    "last_updated_date",
    "last_updated_by",
    "ts_event_notification_time"
]

# Create dataframe using json_normalize pandas function with necessary parameters
df = pd.json_normalize(data['OrderMaster']['Order']['item'],['OrderItems', 'item'], features)

结果是每个项目的完整行数据:

【讨论】:

以上是关于将嵌套的 Json 文件展平为 Pandas 数据框的主要内容,如果未能解决你的问题,请参考以下文章

将嵌套的 dict 列表展平为 Pandas Dataframe

在熊猫数据框中展平嵌套的 Json

从嵌套的 json 列表中展平 Pandas DataFrame

pandas json_normalize 展平嵌套字典

pandas json_normalize 所有列都有嵌套字典展平

在R中展平深层嵌套的json