我试图从分层DataFrame(python 3.5)创建一个嵌套的JSON对象,将其馈入JavaScript以呈现组织结构图。我基本上是试图建立在这个问题的答案中发现的结构:Organization chart - tree, online, dynamic, collapsible, pictures - in D3从Pandas为组织结构图创建嵌套的JSON
一个例子数据框:
df = pd.DataFrame({\
'Manager_Name':['Mike' ,'Jon', 'Susan' ,'Susan' ,'Joe'],\
'Manager_Title':['Level1' ,'Level2' ,'Level3' ,"Level3", 'Level4'],\
'Employee_Name':['Jon' ,'Susan' ,'Josh' ,'Joe' ,'Jimmy'],\
'Employee_Title':["Level2" ,"Level3" ,"Level4" ,"Level4" ,"Level5"]})
所需的输出将是:
"Name": "Mike"
"Title": "Level1"
"Employees": [{
"Name": "Jon"
"Title": "Level2"
"Employees": [{
"Name": "Susan"
"Title": "Level3"
"Employees": [{
...
...
...
}]
}]
}]
我知道这ISN代码生成服务,但我尝试应用其他类似的相关答案,似乎无法在这里应用这些答案。我也没有太多的字典(我更像是一个R人),所以这个问题可能有些不好。我有更多的时间比我应该在这个,但我敢肯定这里有人可以在几分钟内做到这一点。
其他问题:
- pandas groupby to nested json
- Creating nested Json structure with multiple key values in Python from Json
- How to build a JSON file with nested records from a flat data table?
提前感谢!
什么是根元素的标准是什么?它是'Title =='Level1'吗? –