def make_node_df(G):
nodes = {}
for node, attribute in G.nodes(data=True):
if not nodes.get('node'):
nodes['node'] = [node]
else:
nodes['node'].append(node)
for key, value in attribute.items():
if not nodes.get(key):
nodes[key] = [value]
else:
nodes[key].append(value)
return pd.DataFrame(nodes)
def make_edge_df(G):
edges = {}
for source, target, attribute in G.edges(data=True):
if not edges.get('source'):
edges['source'] = [source]
else:
edges['source'].append(source)
if not edges.get('target'):
edges['target'] = [target]
else:
edges['target'].append(target)
for key, value in attribute.items():
if not edges.get(key):
edges[key] = [value]
else:
edges[key].append(value)
return pd.DataFrame(edges)
def node_df_to_ebunch(df, nodename='node'):
attributes = [col for col in df.columns if not col==nodename]
ebunch = []
for ix, row in df.iterrows():
ebunch.append((row[nodename], {attribute:row[attribute] for attribute in attributes}))
return ebunch
So, you can convert the nodes of the Graph to a dataframe with make_node_df, and the edges with make_edge_df.
If you want to go from a dataframe to a graph, you can use the inbuilt function nx.from_pandas_edgelist with the edge dataframe, and for the nodes, you can do:
G = nx.Graph()
nodes = node_df_to_ebunch(df, nodename='node')
G.add_nodes_from(nodes)