我需要将网络节点的程度(以及其他中心性度量标准)输出到.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
答案 0 :(得分:1)
好,在阅读了以上评论(@Joel)并进行了little research之后,我们可以使用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