![](/img/trans.png)
[英]Add Nodes with attributes from a .txt file with multiple delimiters networkx/pandas
[英]NetworkX / Pandas - can't output the degree of the nodes into a .txt file (error message)
我需要将网络节点的度数(以及其他中心性度量)输出到 .txt 文件中。 我可以在以前的 NetworkX / Pandas 版本中做到这一点,但现在出现错误。
我正在使用 NetworkX 版本。 2.1 和 Pandas 版本。 0.23.4:
import pandas as pd
import networkx as nx
G = nx.Graph()
G.add_edges_from([(1,2),(1,3),(2,3),(3,4),(4,5),(4,6)])
df = pd.DataFrame(dict(
DEGREE = nx.degree(G),
DEGREE_CENTRALITY = nx.degree_centrality(G),
EIGENVECTOR = nx.eigenvector_centrality(G),
KATZ = nx.katz_centrality_numpy(G),
CLOSENESS_CENTRALITY = nx.closeness_centrality(G),
BETWEENNESS_CENTRALITY = nx.betweenness_centrality(G),
CLUSTCOEF = nx.clustering(G),
))
#df.index += 1
#df.to_csv('centrality-metrics.csv')
错误信息是:
Traceback (most recent call last):
File "<stdin>", line 8, in <module>
File "/home/arthur/anaconda2/lib/python2.7/site-packages/pandas/core/frame.py", line 348, in __init__
mgr = self._init_dict(data, index, columns, dtype=dtype)
File "/home/arthur/anaconda2/lib/python2.7/site-packages/pandas/core/frame.py", line 459, in _init_dict
return _arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
File "/home/arthur/anaconda2/lib/python2.7/site-packages/pandas/core/frame.py", line 7356, in _arrays_to_mgr
index = extract_index(arrays)
File "/home/arthur/anaconda2/lib/python2.7/site-packages/pandas/core/frame.py", line 7405, in extract_index
raise ValueError('Mixing dicts with non-Series may lead to '
ValueError: Mixing dicts with non-Series may lead to ambiguous ordering.
更新:如果我注释(#) DEGREE 行,则不会发生错误。 输出 .txt 与此类似(我的原始数据来自基因网络):
GENE BETWEENNESS CLOSENESS CLUSTCOEF DEGREE DEGREE_CENTRALITY EIGENVECTOR KATZ
A1BG 0.000142303010695361 0.318553702985653 0.0350877192982456 19 0.00115635080031647 7.26316435442522e-05 0.00556190357079266
A1CF 5.71271005407417e-05 0.33571705861921 0.375324675324675 56 0.00340819183251172 0.000225887126821305 -0.00251062164644857
A2M 0.000886299232394493 0.370534908894101 0.109265734265734 146 0.00888564299190554 0.00294316042120819 0.0163918804690203
A4GALT 7.38971769935498e-07 0.286369102602088 0 4 0.000243442273750837 1.35050378606586e-05 -0.000361261465931375
A4GNT 1.03038198147882e-05 0.297404430929626 0 15 0.000912908526565638 2.45985150882602e-05 0.00562955611859571
好的,在阅读上述评论(@Joel)并进行一些研究后,我们可以使用dict(G.degree)
而不是nx.degree
:
import networkx as nx
G = nx.Graph()
G.add_edges_from([(1,2),(1,3),(2,3),(3,4),(4,5),(4,6)])
df = pd.DataFrame(dict(
DEGREE = dict(G.degree),
DEGREE_CENTRALITY = nx.degree_centrality(G),
EIGENVECTOR = nx.eigenvector_centrality(G),
KATZ = nx.katz_centrality_numpy(G),
CLOSENESS_CENTRALITY = nx.closeness_centrality(G),
BETWEENNESS_CENTRALITY = nx.betweenness_centrality(G),
CLUSTCOEF = nx.clustering(G),
))
输出:
DEGREE DEGREE_CENTRALITY EIGENVECTOR KATZ CLOSENESS_CENTRALITY BETWEENNESS_CENTRALITY CLUSTCOEF
1 2 0.4 0.456984 0.408764 0.500000 0.0 1.000000
2 2 0.4 0.456984 0.408764 0.500000 0.0 1.000000
3 3 0.6 0.584217 0.448883 0.714286 0.6 0.333333
4 3 0.6 0.417120 0.441314 0.714286 0.7 0.000000
5 1 0.2 0.183076 0.367131 0.454545 0.0 0.000000
6 1 0.2 0.183076 0.367131 0.454545 0.0 0.000000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.