Python:如何从熊猫数据框创建字典?

时间:2018-10-26 14:22:30

标签: python dictionary networkx

我想按照here的说明使用networkx在python中执行个性化的页面排名(ppr)

我有一个数据框,其中包含两个节点ij之间的边缘列表

df
     i     j
0    A     B
1    B     C
2    B     A
3    C     A
4    C     B

,对于每个节点,我都有一个标签01

dfN 
      Node  Label
0      A     0
1      B     1
2      C     0

我创建了网络

import networkx as nx
g = nx.from_pandas_edgelist(df, 'A', 'B')

现在我要运行ppr

ppr1 = nx.pagerank(g,personalization={A:0, B:1, C:0})

如何直接从dict创建{A:0, B:1, C:0} dfN

1 个答案:

答案 0 :(得分:2)

您可以使用内置方法to_dict

pd.Series([0, 1, 1, 0], index=[0, 1, 2,3]).to_dict()
Out[39]: {0: 0, 1: 1, 2: 1, 3: 0}