将多索引数据框转换为json

时间:2020-02-17 13:59:58

标签: python json pandas

这是多索引数据帧

accounting                                         sales
PhNumber    age     firstName      lastName         PhNumber       age  firstName   lastName
0   <PH_Number> 29  <first_Name>    <last_Name>     <PH_Number> 29  <first_Name>    <last_Name>
1   <PH_Number> 38  <first_Name>    <last_Name>     <PH_Number> 48  <first_Name>    <last_Name>

如何将其转换为正确的json? 我使用过pandas.to_json()。 但无法获得所需的输出

{ "accounting": [{"firstName": <first_name>,
   "lastName": <last_name>,
   "age": 29,
   "PhNumber": <PH_Number>},
  {"firstName": <first_name>,
   "lastName": "<last_name>",
   "age": 38,
   "PhNumber": <PH_Number>}],
 "sales": [{"firstName": "<first_name>",
   "lastName": "<last_name>",
   "age": 29,
   "PhNumber": <PH_Number>},
  {"firstName": "<first_name>",
   "lastName": "<last_name>",
   "age": 48,
   "PhNumber": <PH_Number>}]}

1 个答案:

答案 0 :(得分:1)

您的要求超出了to_json的范围,因此您应该首先计算Python数据结构,然后将其转换为JSON:

data_struct = {k: df[k].to_dict(orient='records') for k in df.columns.levels[0]}

然后您可以轻松地构建JSON文件(或字符串):

print(json.dumps(data_struct, indent=2)

给予:

{
  "accounting": [
    {
      "PhNumber": "<PH_Number>",
      "age": 29,
      "firstName": "<first_Name>",
      "lastName": "<last_Name>"
    },
    {
      "PhNumber": "<PH_Number>",
      "age": 38,
      "firstName": "<first_Name>",
      "lastName": "<last_Name>"
    }
  ],
  "sales": [
    {
      "PhNumber": "<PH_Number>",
      "age": 29,
      "firstName": "<first_Name>",
      "lastName": "<last_Name>"
    },
    {
      "PhNumber": "<PH_Number>",
      "age": 48,
      "firstName": "<first_Name>",
      "lastName": "<last_Name>"
    }
  ]
}