用户定义的Jand格式来自Pandas DataFrame

时间:2016-02-29 11:11:06

标签: python json pandas dataframe

我有一个pandas dataFrame。在打印pandas DataFrame后,结果如下所示

country     branch      no_of_employee     total_salary    count_DOB   count_email
  x            a            30                 2500000        20            25
  x            b            20                 350000         15            20
  y            c            30                 4500000        30            30
  z            d            40                 5500000        40            40
  z            e            10                 1000000        10            10
  z            f            15                 1500000        15            15

我想将其转换为用户定义的用户格式,如

    {
      "x": [
        {
          "Branch": "a",
          "no_employee": 30
        },
        {
          "Branch": "b",
          "no_employee": 20
        }

      ],
      "y": [
         {
          "Branch": "c",
          "no_employee": 30
        },
        {
          "Branch": "d",
          "no_employee": 40
        }

      ],
      "z": [
         {
          "Branch": "d",
          "no_employee": 40
        },
        {
          "Branch": "e",
          "no_employee": 10
        },
        {
          "Branch": "f",
          "no_employee": 15
        }

  ]

}

如何将此dataFrame转换为此格式

1 个答案:

答案 0 :(得分:3)

您可以使用groupby applyto_dict来尝试to_json

g = df.groupby('country')[["branch", "no_of_employee"]]
                                                .apply(lambda x: x.to_dict(orient='records'))
print g.to_json()

{
    "x": [{
        "no_of_employee": 30,
        "branch": "a"
    }, {
        "no_of_employee": 20,
        "branch": "b"
    }],
    "y": [{
        "no_of_employee": 30,
        "branch": "c"
    }],
    "z": [{
        "no_of_employee": 40,
        "branch": "d"
    }, {
        "no_of_employee": 10,
        "branch": "e"
    }, {
        "no_of_employee": 15,
        "branch": "f"
    }]
}