数据框中的节点特征到 networkx 图列表
Node features in dataframe to networkx list of graphs
我有一个 networkx 图列表。我有一个包含以下信息的数据框:
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]
两列包含相同的信息,但一列是列表,一列是字典。我正在尝试遍历每个图及其各自的节点并添加相关的节点功能。
例如图[0]在df['ln'或'ln2'][0]中有节点特征。我试过:
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]
并收到:
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
我也尝试过对单个图进行此操作:
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'
与字典相同的结果。
我在分配数据之前更改了我的节点 ID,因此它无法识别新 ID。
我有一个 networkx 图列表。我有一个包含以下信息的数据框:
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]
两列包含相同的信息,但一列是列表,一列是字典。我正在尝试遍历每个图及其各自的节点并添加相关的节点功能。
例如图[0]在df['ln'或'ln2'][0]中有节点特征。我试过:
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]
并收到:
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
我也尝试过对单个图进行此操作:
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'
与字典相同的结果。
我在分配数据之前更改了我的节点 ID,因此它无法识别新 ID。