【问题标题】:Make NetworkX node attributes into Pandas Dataframe columns将 NetworkX 节点属性设置为 Pandas Dataframe 列
【发布时间】:2016-05-04 22:04:12
【问题描述】:

我在下面创建了一个名为GNetworkx graph

import networkx as nx
G = nx.Graph()
G.add_node(1,job= 'teacher', boss = 'dee')
G.add_node(2,job= 'teacher', boss = 'foo')
G.add_node(3,job= 'admin', boss = 'dee')
G.add_node(4,job= 'admin', boss = 'lopez')

我想将node 号码与attributesjobboss 一起存储在pandas dataframe 的单独列中。

我尝试使用以下代码执行此操作,但它会生成一个带有 2 列的 dataframe,其中 1 列带有 node 数字,1 列带有所有 attributes

graph = G.nodes(data = True)
import pandas as pd
df = pd.DataFrame(graph)

df
Out[19]: 
    0                                      1
0  1  {u'job': u'teacher', u'boss': u'dee'}
1  2  {u'job': u'teacher', u'boss': u'foo'}
2  3    {u'job': u'admin', u'boss': u'dee'}
3  4  {u'job': u'admin', u'boss': u'lopez'}

注意:我承认NetworkX 具有to_pandas_dataframe 函数,但它没有提供dataframe 以及我正在寻找的输出。

【问题讨论】:

    标签: python python-2.7 pandas dataframe networkx


    【解决方案1】:

    我认为这更简单:

    pandas.DataFrame.from_dict(graph.nodes, orient='index')
    

    无需转换为另一个字典。

    【讨论】:

    • 这不起作用,但如果节点没有属性,那么你会得到一个空的 DataFrame。
    • 我知道这个答案晚了 2 年,但它应该是公认的答案
    【解决方案2】:

    这是一个单线。

    pd.DataFrame.from_dict(dict(graph.nodes(data=True)), orient='index')
    

    【讨论】:

    • 这是更pythonic的答案。
    • 这不起作用,但如果节点没有属性,那么你会得到一个空的 DataFrame。
    • @Mitar 没有属性的图的预期输出是什么?只有索引的数据框?
    • 是的,理想情况下只有索引。
    【解决方案3】:

    我更新了这个解决方案以使用我的 NetworkX (2.0) 的更新版本,并认为我会分享。我还让函数返回一个 Pandas DataFrame。

    def nodes_to_df(graph):
        import pandas as pd
        data={}
        data['node']=[x[0] for x in graph.nodes(data=True)]
        other_cols = graph.nodes[0].keys()
        for key in other_cols:
            data[key] = [x[1][key] for x in graph.nodes(data=True)]
        return pd.DataFrame(data)
    

    【讨论】:

      【解决方案4】:

      我不知道您的数据有多大的代表性,但修改我的代码以在您的真实网络上工作应该很简单:

      In [32]:
      data={}
      data['node']=[x[0] for x in graph]
      data['boss'] = [x[1]['boss'] for x in graph]
      data['job'] = [x[1]['job'] for x in graph]
      df1 = pd.DataFrame(data)
      df1
      
      Out[32]:
          boss      job  node
      0    dee  teacher     1
      1    foo  teacher     2
      2    dee    admin     3
      3  lopez    admin     4
      

      所以我在这里所做的只是从图形数据构造一个字典,pandas 接受字典作为数据,其中键是列名,数据必须是数组,在这种情况下是值列表

      更动态的方法:

      In [42]:
      def func(graph):
          data={}
          data['node']=[x[0] for x in graph]
          other_cols = graph[0][1].keys()
          for key in other_cols:
              data[key] = [x[1][key] for x in graph]
          return data
      pd.DataFrame(func(graph))
      
      Out[42]:
          boss      job  node
      0    dee  teacher     1
      1    foo  teacher     2
      2    dee    admin     3
      3  lopez    admin     4
      

      【讨论】:

      • 感谢您的解决方案。我不明白的解决方案的唯一部分是x[0] for x in graph。我知道graphlist,但是xx[0]graph 中发生了什么?
      • 你有一个元组列表,元组中的第一个元素是节点值,因此x[0]第二个元素是一个字典x[1]
      • 有一个错误。应该是def func(graph):
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-28
      • 1970-01-01
      • 2023-03-10
      • 2017-07-05
      • 1970-01-01
      相关资源
      最近更新 更多