【问题标题】:Converting JSON file to Pandas data [closed]将 JSON 文件转换为 Pandas 数据 [关闭]
【发布时间】:2021-09-09 11:31:03
【问题描述】:

到目前为止,我还没有处理过 JSON 文件,我必须解决从包含大量信息的巨大 JSON 文件中过滤一些数据的要求 Q1。我应该将此 JSON 文件更改为 Pandas 数据框并过滤所需的信息 这不是一个糟糕的编码技术吗 Q2 如何在python中直接从JSON文件中过滤数据

JSON 文件的前几行实际上是文件的 30 倍多的代码,不能在这里发布 但是过滤这段代码会让我有直觉来提取和存储信息

      {
      "response":{
    "status_code":200,
    "data":{
        "id":18288,
        "code":"u2vx",
        "accepts_instructions":true,
        "address":"76 Z block commercial market Phase-3 DHA Lahore.",
        "address_line2":"76 Z block commercial market Phase-3 DHA Lahore.",
        "budget":3,
        "chain":{
            "id":2,
            "is_accepting_global_vouchers":true,
            "main_vendor_code":"s0ox",
            "main_vendor_id":1439,
            "name":"KFC",
            "url_key":"kfc",
            "code":"cs5rw"
        },
        "city":{
            "id":200253,
            "name":"Lahore",
            "url_key":"lahore",
            "is_top_city":false,
            "is_main_city":false,
            "is_express_delivery_enabled":false,
            "latitude":31.510849,
            "longitude":74.37538147,
            "timezone":"Asia/Karachi"
        },
        "cuisines":[
            {
                "id":86,
                "name":"Fast Food",
                "url_key":"fast-food",
                "main":true
            }
        ],
        "custom_location_url":"",
        "customer_phone":"+3202440571, 0423-5746464, 0423-5692414, Mr Babar\t0301-4147257, 
           0332-1494447, 0332-1494448",
        "customer_type":"all",
        "delivery_box":"",
        "delivery_conditions":[
            {
                "delivery_fee":0,
                "delivery_fee_type":"amount",
                "minimum_order_amount":0,
                "maximum_order_amount":0
            }
        ],
        "delivery_fee_type":"amount",
        "delivery_fee_source":"disco",
        "delivery_provider_id":0,
        "description":"",
        "discounts":[
            
        ],
        "distance":8533.565,
        "experiments":[
            {
                "experiment_id":"recommendation-products-elements",
                "experiment_variation":"Control",
                "is_participating":false
            },
            {
                "experiment_id":"vendor-details-availability-concurrent",
                "experiment_variation":"Control",
                "is_participating":false
            },
            {
                "experiment_id":"recommendation-products-quantity",
                "experiment_variation":"Control",
                "is_participating":false
            }
        ],
        "food_characteristics":[
            {
                "id":120,
                "name":"Halal",
                "is_halal":true,
                "is_vegetarian":false
            },
            {
                "id":129,
                "name":"foodpanda delivery",
                "is_halal":true,
                "is_vegetarian":false
            }
        ],
        "favorite_data":null,
        "has_delivery_provider":true,
        "hero_image":"https://images.deliveryhero.io/image/fd-pk/LH/u2vx-hero.jpg",
        "hero_listing_image":"https://images.deliveryhero.io/image/fd-pk/LH/u2vx-listing.jpg",
        "is_active":true,
        "is_busy":false,
        "is_best_in_city":false,
        "is_checkout_comment_enabled":true,
        "is_delivery_enabled":true,
        "is_express_delivery_available":false,
        "is_express_delivery_enabled":false,
        "is_new":false,
        "is_new_until":"2019-09-07T00:00:00Z",
        "is_pickup_enabled":false,
        "is_premium":false,
        "premium_position":0,
        "is_preorder_enabled":false,
        "is_promoted":false,
        "is_replacement_dish_enabled":false,
        "is_service_fee_enabled":false,
        "is_service_tax_enabled":false,
        "is_service_tax_visible":false,
        "is_test":false,
        "is_vat_disabled":false,
        "is_vat_included_in_product_price":true,
        "is_vat_visible":false,
        "is_vat_included":true,
        "is_voucher_enabled":false,
        "latitude":31.473286,
        "location_event":null,
        "logo":"",
        "longitude":74.378387,
        "loyalty_percentage_amount":0,
        "loyalty_program_enabled":false,
        "maximum_express_order_amount":0,
        "menus":[
            {
                "id":35895,
                "code":"",
                "name":"Mid Night Menu",
                "description":"",
                "type":"delivery",
                "opening_time":"00:01:00",
                "closing_time":"06:00:00",
                "menu_categories":[
                    {
                        "id":284921,
                        "code":"s6ey-mc-5gy",
                        "name":"Midnight Deals",
                        "description":"(Starting From 12:00am)",
                        "products":[
                            {
                                "id":2148024,
                                "code":"s6ey-pr-owmq",
                                "name":"Midnight Deal 1",
                                "description":"Zinger burger with regular soft drink",
                                "display_price":"",
                                "master_category_id":8,
                                "file_path":"https://images.deliveryhero.io/image/fd- 
                                   pk/Products/1164992.png?width=%s",
                                "logo_path":"https://images.deliveryhero.io/image/fd- 
                               pk/Products/1164992.png?width=%s",
                                "images_urls":[
                                    
                                ],
                                "images":[
                                    {
                                        "image_url":"https://images.deliveryhero.io/image/fd- 
                            pk/Products/1164992.png",
                                        "tags":{
                                            
                                        }
                                    }
                                ],
                                "is_prepacked_item":false,
                    


      

【问题讨论】:

  • 这能回答你的问题吗? JSON to pandas DataFrame
  • 响应数据 {'id': 18288, 'code': 'u2vx', 'accepts_instruc... status_code 200
  • 不,解决方案不合格,输出既不可读也不可编辑,没有键和值的分离

标签: python json pandas numpy operating-system


【解决方案1】:
import pandas as pd
df = pd.read_json(r'directory\Name.json')
df.head()

【讨论】:

  • response data {'id': 18288, 'code': 'u2vx', 'accepts_instruc... status_code 200 //是我得到的所有输出不能做任何操作,需要丢弃一些列
【解决方案2】:

如果您的数据是非结构化的,您必须使用json 打开文件,如下所示:

JSON to pandas DataFrame

否则,如果它已经在表格结构中,请使用 Nikunj 的答案,如下所示:

import pandas as pd
df = pd.read_json(r'directory\Name.json')
df.head()

【讨论】:

    猜你喜欢
    • 2020-12-21
    • 2020-02-29
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-01
    • 2021-12-13
    相关资源
    最近更新 更多