Python 从数据帧创建NetworkX图形

Python 从数据帧创建NetworkX图形,python,pandas,networkx,Python,Pandas,Networkx,我正在尝试构建一个网络,其中节点是人名,边是在关系索引大于0.60的节点/人之间创建的 数据来自大熊猫 Name Relationship index Julie 0.4 Marie 0.2 Bob 0.7 Mark 0.85 Chris 0.43 我所做的是得到表的线性表示: dat = df.set_index('Name').stack() 然后尝试在具有关系索引>0.6的人之间建立联系: dat

我正在尝试构建一个网络,其中节点是人名,边是在关系索引大于
0.60
的节点/人之间创建的

数据来自大熊猫

Name      Relationship index
Julie        0.4
Marie        0.2
Bob          0.7 
Mark         0.85
Chris        0.43
我所做的是得到表的线性表示:

dat = df.set_index('Name').stack()
然后尝试在具有
关系索引>0.6的人之间建立联系:

dat = dat[dat['Relationship index']>0.6]
并获取边缘列表:

edges = dat.index.tolist()
然后我将网络构建为二部图:

G = nx.Graph(edges)
Gp = nx.bipartite.project(G, dat.set_index('Name').columns)

Gp.edges()
不幸的是,我遇到了以下错误:

----> 2 dat = dat[dat['Relationship index']>0.6]

AttributeError: 'Series' object has no attribute 'Relationship index'
你能告诉我怎么了吗

预期产出:


Bob和Mark相互连接而其他人断开连接的图形。

代码中不起作用的内容:

dat = df.set_index('Name').stack()
this line gets rid of column names, 
so you cannot access them with ['Relationship index']
anymore
对于您的特定问题,您可以使用itertools:

import itertools

matches = df[df['Relationship index']>.6]['Name'].tolist()
edges = itertools.product(matches, matches)

G = nx.Graph()
G.add_nodes_from(df['Name'])
G.add_edges_from(edges)

nx.draw_networkx(G)

我不确定自己是否完全理解。在您共享的数据中,Mark的关系指数为0.85。但是和谁在一起?嗨,扭曲了。所有关系索引大于0.6的节点都彼此有关系(问题更复杂,这只是一个简化版本;)谢谢@warped。我收到以下错误:
AttributeError:module'matplotlib.cbook'没有属性'is_numlike'
。你认为是由于某个软件包造成的吗?@Math说错误是由于matplotlib以两种不同的方式安装所致。因此,这与networkx以及绘制图形的代码无关。非常感谢@warped