简体   繁体   English

dataframe 中的节点功能到 networkx 图表列表

[英]Node features in dataframe to networkx list of graphs

I have a list of networkx graphs.我有一个 networkx 图列表。 I have a dataframe with the following info:我有一个 dataframe 具有以下信息:

Out[91]: 
                                                      ln                                                ln2
0      [[67, 1], [67, 1], [67, 1], [67, 1], [67, 1], ...  {0: [67, 1], 1: [67, 1], 2: [67, 1], 3: [67, 1...
1      [[67, 1], [67, 1], [67, 1], [67, 1], [67, 1], ...  {0: [67, 1], 1: [67, 1], 2: [67, 1], 3: [67, 1...
2      [[67, 1], [67, 1], [67, 1], [67, 1], [67, 1], ...  {0: [67, 1], 1: [67, 1], 2: [67, 1], 3: [67, 1...
3      [[67, 1], [67, 1], [67, 1], [67, 1], [67, 1], ...  {0: [67, 1], 1: [67, 1], 2: [67, 1], 3: [67, 1...
4      [[67, 1], [67, 1], [67, 1], [67, 1], [67, 1], ...  {0: [67, 1], 1: [67, 1], 2: [67, 1], 3: [67, 1...
                                                  ...                                                ...
43244  [[71, 1], [73, 1], [79, 1], [80, 1], [80, 1], ...  {0: [71, 1], 1: [73, 1], 2: [79, 1], 3: [80, 1...
43245  [[71, 1], [73, 1], [79, 1], [80, 1], [80, 1], ...  {0: [71, 1], 1: [73, 1], 2: [79, 1], 3: [80, 1...
43246  [[71, 1], [73, 1], [79, 1], [80, 1], [80, 1], ...  {0: [71, 1], 1: [73, 1], 2: [79, 1], 3: [80, 1...
43247  [[71, 1], [73, 1], [79, 1], [80, 1], [80, 1], ...  {0: [71, 1], 1: [73, 1], 2: [79, 1], 3: [80, 1...
43248  [[71, 1], [73, 1], [79, 1], [80, 1], [82, 1], ...  {0: [71, 1], 1: [73, 1], 2: [79, 1], 3: [80, 1...
[43249 rows x 2 columns]

Both columns contain the same information, but ones a list and ones a dict.两列都包含相同的信息,但一个是列表,一个是字典。 I am trying to iterate over every graph and their respective nodes and adding there associated node feature.我正在尝试遍历每个图及其各自的节点并在那里添加相关的节点功能。

For instance graph[0] has node features in df['ln' or 'ln2'][0].例如,graph[0] 在 df['ln' 或 'ln2'][0] 中有节点特征。 I have tried:我努力了:

for i in range(len(graphs)):
    for j in range(len(df['ln'][i])):
        for node_data in graphs[i].nodes(data=True):
            node_data['feature'] = df['ln'][i][j]

and received:并收到:

Traceback (most recent call last):
  File "/home/anthony/anaconda3/envs/GeoDL/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3427, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-75-894a20b5da94>", line 4, in <module>
    node_data['feature'] = df['ln'][i][j]
TypeError: 'tuple' object does not support item assignment

I have also tried to do it to a single graph:我也尝试对单个图表执行此操作:

g0.nodes.data(df['ln'][0])
Out[88]: Traceback (most recent call last):
  File "/home/anthony/anaconda3/envs/GeoDL/lib/python3.8/site-packages/IPython/core/formatters.py", line 224, in catch_format_error
    r = method(self, *args, **kwargs)
  File "/home/anthony/anaconda3/envs/GeoDL/lib/python3.8/site-packages/IPython/core/formatters.py", line 702, in __call__
    printer.pretty(obj)
  File "/home/anthony/anaconda3/envs/GeoDL/lib/python3.8/site-packages/IPython/lib/pretty.py", line 394, in pretty
    return _repr_pprint(obj, self, cycle)
  File "/home/anthony/anaconda3/envs/GeoDL/lib/python3.8/site-packages/IPython/lib/pretty.py", line 700, in _repr_pprint
    output = repr(obj)
  File "/home/anthony/anaconda3/envs/GeoDL/lib/python3.8/site-packages/networkx/classes/reportviews.py", line 302, in __repr__
    return f"{name}({dict(self)}, data={self._data!r})"
  File "/home/anthony/anaconda3/envs/GeoDL/lib/python3.8/site-packages/networkx/classes/reportviews.py", line 268, in <genexpr>
    (n, dd[data] if data in dd else self._default)
TypeError: unhashable type: 'list'

The same result with the dict.与 dict 的结果相同。

I changed my node IDs prior to assigning the data, therefore it did not recognize the new IDs.我在分配数据之前更改了我的节点 ID,因此它无法识别新的 ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM