<p><a href="http://networkx.github.io/documentation/latest/reference/generated/networkx.convert.from_numpy_matrix.html">NetworkX expects a square matrix</a>(节点和边),可能*您想传递它:</p>
<pre><code>In [11]: df2 = pd.concat([df, df.T]).fillna(0)
</code></pre>
<p><em>注意:索引和列的顺序必须相同!</em></p>
<pre><code>In [12]: df2 = df2.reindex(df2.columns)
In [13]: df2
Out[13]:
Bar Bat Baz Foo Loc 1 Loc 2 Loc 3 Loc 4 Loc 5 Loc 6 Loc 7 Quux
Bar 0 0 0 0 0 0 1 1 0 1 1 0
Bat 0 0 0 0 0 0 1 0 0 1 0 0
Baz 0 0 0 0 0 0 1 0 0 0 0 0
Foo 0 0 0 0 0 0 1 1 0 0 0 0
Loc 1 0 0 0 0 0 0 0 0 0 0 0 1
Loc 2 0 0 0 0 0 0 0 0 0 0 0 0
Loc 3 1 1 1 1 0 0 0 0 0 0 0 0
Loc 4 1 0 0 1 0 0 0 0 0 0 0 0
Loc 5 0 0 0 0 0 0 0 0 0 0 0 0
Loc 6 1 1 0 0 0 0 0 0 0 0 0 0
Loc 7 1 0 0 0 0 0 0 0 0 0 0 0
Quux 0 0 0 0 1 0 0 0 0 0 0 0
In[14]: graph = nx.from_numpy_matrix(df2.values)
</code></pre>
<p>如果您想使用<a href="http://networkx.github.io/documentation/latest/reference/generated/networkx.relabel.relabel_nodes.html">^{<cd1>}</a>来传递列/索引名,则这不会将列/索引名传递给图形(您可能必须小心重复项,这在pandas的数据帧中是允许的):</p>
<pre><code>In [15]: graph = nx.relabel_nodes(graph, dict(enumerate(df2.columns))) # is there nicer way than dict . enumerate ?
</code></pre>
<p><em>*目前还不清楚列和索引究竟代表了所需图形的什么。</em></p>