如何在数据框中存储 "networkx info" 输出

how to store "networkx info" output in a data frame

我想将以下 NetworkX 输出的输出存储到 Pandas 数据框中:

for i in (node_id):
    G.remove_nodes_from([i])
    (nx.info(G))

当前输出如下所示:

Name: 
Type: Graph
Number of nodes: 262
Number of edges: 455
Average degree:   3.4733
Name: 
Type: Graph
Number of nodes: 261
Number of edges: 425
Average degree:   3.2567

拜托,你能告诉我一种将这些输出存储到数据框或字典中的方法吗

nx.info输出一个字符串,你可以把它喂给pandas.read_csv:

import networkx as nx
import io
import pandas as pd

# dummy graph
G = nx.star_graph(5)

df = pd.read_csv(io.StringIO(nx.info(G)), sep=':\s*', engine='python', names=['attribute', 'value'])
print(df)

输出:

         attribute   value
0             Name     NaN
1             Type   Graph
2  Number of nodes       6
3  Number of edges       5
4   Average degree  1.6667

注意。请注意,nx.info 已弃用,将在 networkx 3

中删除