python - 尝试使用 pyvis 可视化 networkx 图时如何解决此关键错误?
问题描述
您好,我在 python 中有另一个 netowrkx 问题。我正在尝试使用以下函数来创建可视化,但每当我尝试运行以下代码时都会出现 keyerror:
G = nx.Graph()
G = nx.from_pandas_edgelist(
df_edges,'user_id_x','user_id_y')
node_attr = df_nodes.set_index('user_id')
nx.set_node_attributes(G, node_attr)
communities = community.greedy_modularity_communities(G)
modularity_dict = {} # Create a blank dictionary
for i,c in enumerate(communities): # Loop through the list of communities, keeping track of the number for the community
for name in c: # Loop through each person in a community
modularity_dict[name] = i # Create an entry in the dictionary for the person, where the value is which group they belong to.
# Now you can add modularity information like we did the other metrics
nx.set_node_attributes(G, modularity_dict, 'modularity')
def visualize(identity_id,html_name):
#create subgraph using the provided identity_id
classx = [n for n in G.nodes() if G.nodes[n]['modularity'] ==
G.nodes[identity_id]['modularity']]
SG = G.subgraph(classx)
#instantiate the Network object
N = Network(height='800px', width='100%', bgcolor='#ffffff',
font_color='black',notebook = True, directed=False)
#this line effects the physics of the html File
N.barnes_hut(spring_strength=0.006)
for n in SG:
if ((SG.nodes[n]['category']=='cust') and (SG.nodes[n]['status']=='ACTIVE')): # assign color to nodes based on cust status
color = 'green'
shape = 'square'
if ((SG.nodes[n]['category']=='cust') and (SG.nodes[n]['status']=='CLOSED')): # assign color to nodes based on cust status
color = 'red'
shape = 'square'
elif SG.nodes[n]['category']=='app':# assign shape to nodes based on cust versus app
color = 'blue'
shape = 'triangle'
N.add_node(n, label=n, color=color,shape = shape)
for e in SG.edges:
if e in SG.edges: # add the edges to the graph
color = 'black'
width = 2
N.add_edge(e[0],e[1],color=color, width=width)
N.show_buttons(filter_=True)
N.show(f'subgraph_{html_name}.html') # save a html file in current dir
print(f"Number of nodes in this customer's device based network: {SG.number_of_nodes()}")
print('\nList of associated customers and applications based on device:\n')
display(df_nodes[(df_nodes['user_id'].isin(list(SG.nodes)))])#print list of customer_ids that are associated with the customer
network_df= df_nodes[(df_nodes['user_id'].isin(list(SG.nodes)))]
visualize('b572f4d3-2c69-48fa-91fe-849fa55d1be7','customerA')
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-124-d0c830d4c4a1> in <module>
----> 1 visualize('b572f4d3-2c69-48fa-91fe-849fa55d1be7','customerA')
<ipython-input-123-5d1d5e9d24a4> in visualize(identity_id, html_name)
14
15 for n in SG:
---> 16 if ((SG.nodes[n]['category']=='cust') and (SG.nodes[n]['status']=='ACTIVE')): # assign color to nodes based on cust status
17 color = 'green'
18 shape = 'square'
KeyError: 'category'
这非常令人沮丧,因为上周这段代码运行良好,而且我很肯定我没有改变任何关键的东西。
对于那些好奇的人来说,这就是 df_nodes DataFrame 的样子:
这特别奇怪,因为当我在函数之外打印出节点属性时,它会返回预期的结果。
先感谢您!
解决方案
推荐阅读
- powershell - 从 Process 块调用函数
- java - Spring vs Spring boot 反向代理问题
- reactjs - 如何使用 Axios 和 React 传递 Header JWT 令牌?
- tensorflow - 为什么 tf.cond 中的 false_fn 从未调用过?
- javascript - 我正在尝试使用 JavaScript 对网页进行密码保护
- uwsgi - 为什么我应该使用 max-request 来避免内存泄漏?
- libgdx - Godot 或 LibGDX 用于 2d 游戏
- jquery - jQuery 顶层 $(this) 的含义
- dart - Flutter 中的 StringBuffer 无法按预期工作
- javascript - 点燃元素示例中“$=”或“?=”的用途