如何在python 2.7中将eigenvector_centrality的结果导出为networkx的csv文件?

时间:2014-09-01 22:21:50

标签: python ubuntu networkx

我是python的新手并尝试使用networkx来计算图形的eigenvector_centrality,这是一个像这样的file.csv:

erwr,dfas,3
rfrf,g2rf,5
fewe,fef2,2

前两列作为节点,第三列作为边的权重。

我运行以下代码来获取图表eigenvector_centrality的结果:

python
import networkx as nx
G = nx.read_weighted_edgelist("file.csv", delimiter=",") 
G.edges(data=True)
centrality = nx.eigenvector_centrality(G)
print(['%s %0.2f'%(node,centrality[node]) for node in centrality])

我想知道如何导出eigenvector_centrality的结果,因为result.csv文件的格式如下:

erwr,0.42
rfrf,0.38
fewe,0.32

第一列作为节点,第二列作为其中心性?我正在使用ubuntu和python2.7

提前!

1 个答案:

答案 0 :(得分:2)

In [1]: import networkx as nx

In [2]: edges = """erwr,dfas,3
rfrf,g2rf,5
fewe,fef2,2"""

In [3]: G = nx.parse_edgelist(edges.split('\n'),delimiter=',',  data=(('weight',float),))

In [4]: centrality = nx.eigenvector_centrality(G)

In [5]: for node in centrality:
    print("%s,%0.2f"%(node,centrality[node]))
   ...:     
g2rf,0.71
dfas,0.00
fewe,0.00
rfrf,0.71
erwr,0.00
fef2,0.00