我有一个如下所示的数据框架。
Supervisor-L3 Supervisor-L2 Supervisor-L1 Employee
O M J A
O M J B
O M J C
O M K D
O N K E
O N K F
O N L G
O N L H
O N L I
我想将数据帧转换为json文件,以生成组织结构图。但是,当我使用pandas.to_json函数时。输出为:
{"Supervisor-L3":{"0":"O","1":"O","2":"O","3":"O","4":"O","5":"O","6":"O","7":"O","8":"O"},"Supervisor-L2":{"0":"M","1":"M","2":"M","3":"M","4":"N","5":"N","6":"N","7":"N","8":"N"},"Supervisor-L1":{"0":"J","1":"J","2":"J","3":"K","4":"K","5":"K","6":"L","7":"L","8":"L"},"Name":{"0":"A","1":"B","2":"C","3":"D","4":"E","5":"F","6":"G","7":"H","8":"I"}}
我需要一个json文件来描述数据集中人员之间的层次关系。有谁能帮我吗?谢谢!
发布于 2018-07-27 06:09:51
您可以使用networkx或只是将数据拉长为数据,即dataframe。
data = pd.concat([pd.DataFrame(df.iloc[:,i:i+2].values, columns=['P','C']) for i in range(3)], ignore_index=True)
G = nx.from_pandas_edgelist(data, 'P','C')
from networkx.readwrite import json_graph
txtgraph = json_graph.node_link_data(G)
txtgraph
输出:
{'directed': False,
'graph': {},
'links': [{'source': 'O', 'target': 'M'},
{'source': 'O', 'target': 'N'},
{'source': 'M', 'target': 'J'},
{'source': 'M', 'target': 'K'},
{'source': 'N', 'target': 'K'},
{'source': 'N', 'target': 'L'},
{'source': 'J', 'target': 'A'},
{'source': 'J', 'target': 'B'},
{'source': 'J', 'target': 'C'},
{'source': 'K', 'target': 'D'},
{'source': 'K', 'target': 'E'},
{'source': 'K', 'target': 'F'},
{'source': 'L', 'target': 'G'},
{'source': 'L', 'target': 'H'},
{'source': 'L', 'target': 'I'}],
'multigraph': False,
'nodes': [{'id': 'O'},
{'id': 'M'},
{'id': 'N'},
{'id': 'J'},
{'id': 'K'},
{'id': 'L'},
{'id': 'A'},
{'id': 'B'},
{'id': 'C'},
{'id': 'D'},
{'id': 'E'},
{'id': 'F'},
{'id': 'G'},
{'id': 'H'},
{'id': 'I'}]}
发布于 2018-07-27 23:47:33
我将'Supervisor-L3‘的名称修改为'Supervisor’,将'Supervisor-L2‘修改为'Team Leader’,将'Supervisor-L1‘修改为'Company’。因为一个公司可能属于多个团队领导。因此,我编写了三个循环来实现能够描述这些关系的json文件。
a = {'name':'O',
'Subordinate':[]}
##merge these columns to have a one-to-one mapping
df['merge'] = df['Team Leader']+','+df['Company']
df['merge2'] = df['Team Leader']+','+df['Company'] +','+df['Name']
##get the list of unique elements
set1 = list(set(df['Supervisor']))
set2 = list(set(df['Team Leader']))
set3 = list(set(df['merge']))
set4 = list(set(df['merge2']))
## write the loop
for i in range(len(set2)):
temp_dict1 = {'name':set2[i],
'Subordinate':[]}
a['Subordinate'].append(temp_dict1)
m = -1
for j in range(len(set3)):
list1 = set3[j].split(",")
if set2[i] == list1[0]:
temp_dict2 = {'name':list1[1],
'Subordinate':[]}
a['Subordinate'][i]['Subordinate'].append(temp_dict2)
m += 1
for k in range(len(set4)):
list2 = set4[k].split(",")
if (list1[0] == list2[0]) and (list1[1] == list2[1]):
temp_dict3 = {'name':list2[2]}
a['Subordinate'][i]['Subordinate'][m]['Subordinate'].append(temp_dict3)
输出:
Out[86]:
{'Subordinate': [{'Subordinate': [{'Subordinate': [{'name': 'F'},
{'name': 'E'}],
'name': 'K'},
{'Subordinate': [{'name': 'I'}, {'name': 'H'}, {'name': 'G'}],
'name': 'L'}],
'name': 'N'},
{'Subordinate': [{'Subordinate': [{'name': 'D'}], 'name': 'K'},
{'Subordinate': [{'name': 'B'}, {'name': 'A'}, {'name': 'C'}],
'name': 'J'}],
'name': 'M'}],
'name': 'O'}
https://stackoverflow.com/questions/51547857
复制相似问题