Follow-up 在 Dash/Plotly 中显示属性会导致 KeyError
Follow-up to Displaying attributes in Dash/Plotly results in KeyError
在对我的 进行了有用的回答后,我决定使用内部数据(我无法在此处共享)。内部数据遵循与模拟数据相同的格式。我只是将数据复制到同一个工作目录,确保新数据具有相同的格式,即相同的列 headers,等等。我使用 DiffChecker 来确保 app.py
(来自我原来的post) 匹配概念验证 (appPOC.py
)。内部数据有600多个节点,3000多条边
制作交互式仪表板的代码与我用于 的代码相同。但是,这次我 运行 变成了这个 KeyError
:
Traceback (most recent call last):
File "appPOC.py", line 75, in <module>
hovertext = "Document Description: " + str(G.nodes[node]['Description']) + "<br>" + "Document Name: " + str(G.nodes[node]['DocName']) + "<br>" + "Document ID: " + str(G.nodes[node]['DocumentID'])
KeyError: 'Description'
数据本身应该没问题,因为我可以在节点旁边没有悬停文本的情况下绘制网络。
总结一下:app.py
可以绘制模拟数据,appPOC.py
(相同,但文件名不同)不能绘制内部数据。这让我相信 CSV
文件中的内部数据有问题。
编辑: 我发现如果元素中没有列出目标,则无法绘制图形。如果元素中未定义(目标)节点,是否有自动创建节点(如在 Gephi 中)?
NetworkX
为每条边的起始节点和终止节点创建节点。因此,
G = nx.from_pandas_edgelist(edges, 'Source', 'Target')
你的图有所有可能的节点。然而,
nx.set_node_attributes(G, nodes.set_index('Doc')['Description'].to_dict(), 'Description')
nx.set_node_attributes(G, nodes.set_index('Doc')['DocumentID'].to_dict(), 'DocumentID')
您只需为节点数据框中的节点属性填写 'Description'
和 'DocumentID'
。一个简单的解决方法是替换
str(G.nodes[node]['Description'])
和
str(G.nodes[node].get('Description', ''))
'DocName'
和 'DocumentID'
也类似。有关 get 方法的更多信息,请访问:Why dict.get(key) instead of dict[key]?
基本上,我们使用 networkx
使用 dict
来存储值并使用 get
方法,它允许提供默认值。
一个简单的可重现的最小示例
import networkx as nx
g = nx.karate_club_graph()
# all nodes in this graph have the node attribute 'club' filled
# we add a node without this node attribute
g.add_node("Test")
print(g.nodes[0]["club"])
# 'Mr. Hi'
# print(g.nodes["Test"]["club"]
# results in KeyError: 'club'
print(g.nodes["Test"].get("club", ""))
# ''
在对我的 app.py
(来自我原来的post) 匹配概念验证 (appPOC.py
)。内部数据有600多个节点,3000多条边
制作交互式仪表板的代码与我用于 KeyError
:
Traceback (most recent call last):
File "appPOC.py", line 75, in <module>
hovertext = "Document Description: " + str(G.nodes[node]['Description']) + "<br>" + "Document Name: " + str(G.nodes[node]['DocName']) + "<br>" + "Document ID: " + str(G.nodes[node]['DocumentID'])
KeyError: 'Description'
数据本身应该没问题,因为我可以在节点旁边没有悬停文本的情况下绘制网络。
总结一下:app.py
可以绘制模拟数据,appPOC.py
(相同,但文件名不同)不能绘制内部数据。这让我相信 CSV
文件中的内部数据有问题。
编辑: 我发现如果元素中没有列出目标,则无法绘制图形。如果元素中未定义(目标)节点,是否有自动创建节点(如在 Gephi 中)?
NetworkX
为每条边的起始节点和终止节点创建节点。因此,
G = nx.from_pandas_edgelist(edges, 'Source', 'Target')
你的图有所有可能的节点。然而,
nx.set_node_attributes(G, nodes.set_index('Doc')['Description'].to_dict(), 'Description')
nx.set_node_attributes(G, nodes.set_index('Doc')['DocumentID'].to_dict(), 'DocumentID')
您只需为节点数据框中的节点属性填写 'Description'
和 'DocumentID'
。一个简单的解决方法是替换
str(G.nodes[node]['Description'])
和
str(G.nodes[node].get('Description', ''))
'DocName'
和 'DocumentID'
也类似。有关 get 方法的更多信息,请访问:Why dict.get(key) instead of dict[key]?
基本上,我们使用 networkx
使用 dict
来存储值并使用 get
方法,它允许提供默认值。
一个简单的可重现的最小示例
import networkx as nx
g = nx.karate_club_graph()
# all nodes in this graph have the node attribute 'club' filled
# we add a node without this node attribute
g.add_node("Test")
print(g.nodes[0]["club"])
# 'Mr. Hi'
# print(g.nodes["Test"]["club"]
# results in KeyError: 'club'
print(g.nodes["Test"].get("club", ""))
# ''