【问题标题】:I want to create a multi nested json from my pandas dataframe我想从我的熊猫数据框创建一个多嵌套的 json
【发布时间】:2019-06-06 08:44:27
【问题描述】:

我有一个以下格式的熊猫数据框:-

    EMPCODE|Indicator|CustNAME
    1        CASA       Raja
    1        CASA       lala
    1        CASA       dada
    1        TL         Nan
    1        l          Nan
    1        p          Nan
    1        q          Nan
    2        CASA       Crick
    2        CASA       Flick
    2        TL         Nan
    2        l          Nan
    2        p          Nan
    2        q          Nan        

我想把它转换成一个嵌套的 json 。

我尝试了各种不同的方法,包括 groupby()、apply(),但我无法获得所需 json 格式的输出。从下面提到的代码中,我得到了两个员工的重复 custNAme 值。

  group = merge_hr_anr.groupby('EMPCODE_y').groups
  group1 = merge_hr_anr.groupby("EMPNAME").groups
    for variable in range(a):
            d = {'EMPCODE_y': list(group.keys())[variable],'EMPNAME': 
            list(group1.keys())[variable] ,'Indicators': [{'IndicatorName': 
            merge_hr_anr.loc[i, 'IndicatorName']} for i in list(group.values()) 
            [variable].unique()]}
            d['Indicators'] = list(map(dict,sorted(set(map(lambda x: 
            tuple(x.items()),d['Indicators'])), key=list(map(lambda x: 
            tuple(x.items()),d['Indicators'])).index)))
            d['Performance'] = [{i['IndicatorName']: 
(merge_hr_anr.loc[merge_hr_anr['IndicatorName'].eq(i['IndicatorName']),"CUSTNAME"]).dropna().tolist()} for i in d['Indicators']]    

    My output is
{
    "EMPCODE": "1",
    "Indicators": [
      {
        "IndicatorName": "CASA"
      },
      {
        "IndicatorName": "TL"
      },
      {
        "IndicatorName": "l"
      },
      {
        "IndicatorName": "p"
      },
      {
        "IndicatorName": "q"
      }
    ]
  "Performance":[
     {
         "CASA":[{"Custname":"Raja"},{"Custname":"lala"},{"Custname":"dada"}]
     },
     {
         "TL":[]
     }
     {
         "l":[]
     }
     {
         "p":[]
     }
     {
         "q":[]
     }

  ]
}
{
    "EMPCODE": "2",
    "Indicators": [
      {
        "IndicatorName": "CASA"
      },
      {
        "IndicatorName": "TL"
      },
      {
        "IndicatorName": "l"
      },
      {
        "IndicatorName": "p"
      },
      {
        "IndicatorName": "q"
      }
    ]
  "Performance":[
     {
         "CASA":[{"Custname":"Raja"},{"Custname":"lala"},{"CustName":"dada"}]
     },
     {
         "TL":[]
     }
     {
         "l":[]
     }
     {
         "p":[]
     }
     {
         "q":[]
     }

  ]
}

我希望输出是

{
    "EMPCODE": "1",
    "Indicators": [
      {
        "IndicatorName": "CASA"
      },
      {
        "IndicatorName": "TL"
      },
      {
        "IndicatorName": "l"
      },
      {
        "IndicatorName": "p"
      },
      {
        "IndicatorName": "q"
      }
    ]
  "Performance":[
     {
         "CASA":[{"Custname":"Raja"},{"Custname":"lala"},{"Custname":"dada"}]
     },
     {
         "TL":[]
     }
     {
         "l":[]
     }
     {
         "p":[]
     }
     {
         "q":[]
     }

  ]
}
{
    "EMPCODE": "2",
    "Indicators": [
      {
        "IndicatorName": "CASA"
      },
      {
        "IndicatorName": "TL"
      },
      {
        "IndicatorName": "l"
      },
      {
        "IndicatorName": "p"
      },
      {
        "IndicatorName": "q"
      }
    ]
  "Performance":[
     {
         "CASA":[{"Custname":"Crick"},{"Custname":"Flick"}]
     },
     {
         "TL":[]
     }
     {
         "l":[]
     }
     {
         "p":[]
     }
     {
         "q":[]
     }

  ]
}

【问题讨论】:

    标签: python pandas to-json


    【解决方案1】:

    尝试使用以下代码构建dictionary:

    group = merge_hr_anr.groupby('EMPCODE').groups
    d = {'EMPCODE': list(group.keys())[0], 'Indicators': [{'IndicatorName': merge_hr_anr.loc[i, 'Indicator']} for i in list(group.values())[0].unique()]}
    d['Indicators'] = list(map(dict,sorted(set(map(lambda x: tuple(x.items()),d['Indicators'])), key=list(map(lambda x: tuple(x.items()),d['Indicators'])).index)))
    d['Performance'] = [{i['IndicatorName']: merge_hr_anr.loc[merge_hr_anr['Indicator'].eq(i['IndicatorName']), 'CustNAME'].dropna().tolist()} for i in d['Indicators']]
    print(d)
    

    输出:

    {'EMPCODE': 1, 'Indicators': [{'IndicatorName': 'CASA'}, {'IndicatorName': 'TL'}, {'IndicatorName': 'l'}, {'IndicatorName': 'p'}, {'IndicatorName': 'q'}], 'Performance': [{'CASA': ['Raja', 'lala', 'dada']}, {'TL': []}, {'l': []}, {'p': []}, {'q': []}]}
    

    要写一个.json 文件:

    with open('outvalue7.json', 'w') as f:
        f.write(str(d))
    

    【讨论】:

    • 您好,谢谢您的回复,但我遇到了问题,我在这里给出的表格只是我必须使用的表格的一个子集。在最初的问题中,我有多个拥有不同客户的员工。所以在迭代时,它会为每个员工显示相同的客户详细信息。
    • 我已经编辑了我的问题,请您仔细阅读并帮助我进行了很多头脑风暴,但找不到解决办法
    猜你喜欢
    • 1970-01-01
    • 2019-01-11
    • 2021-02-04
    • 2017-11-28
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 2023-01-14
    相关资源
    最近更新 更多