0

Python版本:2.7

我正在尝试规范化以下 MongoDB 中可用的 json 数据。为此,我正在使用 json_normalize。但是我无法展平“阶段”元素中的数据。它正在收藏中。当我连续处理这个“阶段”时,它工作正常。如何将阶段内的内容与其余数据一起展平?样本数据如下。

{
"_id" : ObjectId("5a8ffe8aaacee33cd21aabcb"),
"niyoCustomerCompanyId" : 210068,
"version" : 2,
"versionDate" : ISODate("2018-02-23T17:14:10.614+05:30"),
"data" : {
    "CustomerCompanyId" : 210068,
    "companyName" : "XYZ Tractors ",
    "mobileNo" : "***22****",
    "cif" : "25",
    "typeId" : 3,
    "PartnerCompanyId" : "163929",
    "companyDetails" : {
        "sectorType" : "30",
        "sectorSubType" : "2",
        "pincode" : "431602",
        "email" : "abcd@rediffmail.com",
        "stages" : [
            {
                "name" : "Prequalification",
                "status" : "done"
            },
            {
                "name" : "Prequalification1",
                "status" : "pending"
            }
        ],
        "currentStage" : "Pre-Qualification"
    }
  }
 }

我试过的代码如下。

cursor_only_data_stage = customer_collection.find({})
stage_data = list(cursor_only_data_stage)
stage_data_df_1 = pd.DataFrame(json_normalize(stage_data))

预期输出如下。

id                         cif  stage              status version versiondate
5a8ffe8aaacee33cd21aabcb    25  Prequalification    done    2   2018-02-23T17:14:10.614+05:30
5a8ffe8aaacee33cd21aabcb    25  Prequalification1   pending 2   2018-02-23T17:14:10.614+05:30
4

1 回答 1

0

我能够做到

        all_stage_data_flat = pd.DataFrame(json_normalize(all_stage_data_list, [['data','companyDetails', 'stages']],
                                                      ['_id','version','versionDate',['data','cif']]))
于 2018-04-09T10:55:39.543 回答