我有一个称为Gr_Team
的源,目标和权重数据框,看起来像这样(示例)-
Passer Receiver count
116643 102747 27
102826 169102 10
116643 102826 7
167449 102826 8
102747 167449 4
每个通过者和接收者都有自己独特的x,y坐标,我在字典中有
loc
-{'102739': [32.733999999999995, 26.534], '102747': [81.25847826086964, 27.686739130434784], '102826': [68.09609195402302, 77.52206896551728]}
我使用networkx进行了绘制:G=nx.from_pandas_edgelist(Gr_Team, 'Passer', 'Receiver', create_using=nx.DiGraph())
nx.draw(G, loc, with_labels=False, node_color='red',
node_size=Gr_Team['count']*100,
width=Gr_Team['count'],
edge_color = Gr_Team["count"],
edge_cmap = cmap,
arrowstyle='->',
arrowsize=10,
vmin=vmin,
vmax=vmax,
font_size=10,
font_weight="bold",
connectionstyle='arc3, rad=0.1')
这没有任何问题,这就是我得到的:
但是,一旦我尝试过滤出计数值低于一个常量的所有行,就说3,使用此
Gr_Team = Gr_Team[Gr_Team["count"]>3]
,我得到一个关键错误,这是整个错误和回溯,我无法做出任何贡献:Warning (from warnings module):
File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37\lib\site-packages\networkx\drawing\nx_pylab.py", line 676
if cb.iterable(node_size): # many node sizes
MatplotlibDeprecationWarning:
The iterable function was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use np.iterable instead.
Traceback (most recent call last):
File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37\PassMapOptaF24Networkx.py", line 148, in <module>
font_weight="bold")#,
File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37\lib\site-packages\networkx\drawing\nx_pylab.py", line 128, in draw
draw_networkx(G, pos=pos, ax=ax, **kwds)
File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37\lib\site-packages\networkx\drawing\nx_pylab.py", line 280, in draw_networkx
edge_collection = draw_networkx_edges(G, pos, arrows=arrows, **kwds)
File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37\lib\site-packages\networkx\drawing\nx_pylab.py", line 684, in draw_networkx_edges
arrow_color = edge_cmap(color_normal(edge_color[i]))
File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\series.py", line 868, in __getitem__
result = self.index.get_value(self, key)
File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\indexes\base.py", line 4375, in get_value
tz=getattr(series.dtype, 'tz', None))
File "pandas\_libs\index.pyx", line 81, in pandas._libs.index.IndexEngine.get_value
File "pandas\_libs\index.pyx", line 89, in pandas._libs.index.IndexEngine.get_value
File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 987, in pandas._libs.hashtable.Int64HashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 993, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: 1
我意识到仅执行
nx.draw(G, loc, with_labels=False, node_color='red')
仍然有效,但是一旦我尝试传递node_size
或edge_color
,就会遇到上述错误。根据我的理解,仅当我在关键字参数中使用数据框Gr_Team
时才出现错误。我不知道为什么会这样,为什么过滤会破坏代码。任何帮助,将不胜感激。
编辑1 :Here是整个代码的要旨。我尽力使它最小化。 Here是指向csv文件的链接,该文件需要以df格式读取。产生错误的行也在那里。发表评论。
最佳答案
这些错误行提供了很好的提示:
File "pandas_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
KeyError: 1
问题似乎在于,将
Gr_Team
-数据帧传递给指定的node_size
,width
和edge_color
要求索引是连续的。使用Gr_Team = Gr_Team[Gr_Team["count"] > 3 ].reset_index()
应该解决问题。
关于python - 这些networkx错误表示在 Pandas 数据帧上设置过滤器后得到什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56620497/