Converting to and from other data formats¶
To NetworkX Graph¶
Functions to convert NetworkX graphs to and from other formats.
The preferred way of converting data to a NetworkX graph is through the graph constructor. The constructor calls the to_networkx_graph() function which attempts to guess the input type and convert it automatically.
Examples
Create a graph with a single edge from a dictionary of dictionaries
>>> d = {0: {1: 1}} # dict-of-dicts single edge (0,1)
>>> G = nx.Graph(d)
See also
nx_agraph
, nx_pydot
|
Make a NetworkX graph from a known data structure. |
Dictionaries¶
|
Returns adjacency representation of graph as a dictionary of dictionaries. |
|
Returns a graph from a dictionary of dictionaries. |
Lists¶
|
Returns adjacency representation of graph as a dictionary of lists. |
|
Returns a graph from a dictionary of lists. |
|
Returns a list of edges in the graph. |
|
Returns a graph from a list of edges. |
Numpy¶
Functions to convert NetworkX graphs to and from common data containers like numpy arrays, scipy sparse matrices, and pandas DataFrames.
The preferred way of converting data to a NetworkX graph is through the graph constructor. The constructor calls the to_networkx_graph() function which attempts to guess the input type and convert it automatically.
Examples
Create a 10 node random graph from a numpy array
>>> import numpy as np
>>> a = np.random.randint(0, 2, size=(10, 10))
>>> D = nx.DiGraph(a)
or equivalently
>>> D = nx.to_networkx_graph(a, create_using=nx.DiGraph)
See also
nx_agraph
, nx_pydot
|
Returns the graph adjacency matrix as a NumPy matrix. |
|
Returns the graph adjacency matrix as a NumPy array. |
|
Returns the graph adjacency matrix as a NumPy recarray. |
|
Returns a graph from numpy matrix. |
|
Returns a graph from NumPy array. |
Scipy¶
|
Returns the graph adjacency matrix as a SciPy sparse matrix. |
|
Creates a new graph from an adjacency matrix given as a SciPy sparse matrix. |
Pandas¶
|
Returns the graph adjacency matrix as a Pandas DataFrame. |
|
Returns a graph from Pandas DataFrame. |
|
Returns the graph edge list as a Pandas DataFrame. |
|
Returns a graph from Pandas DataFrame containing an edge list. |