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 arrays, 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
>>> rng = np.random.default_rng()
>>> a = rng.integers(low=0, high=2, size=(10, 10))
>>> DG = nx.from_numpy_array(a, create_using=nx.DiGraph)
or equivalently:
>>> DG = nx.DiGraph(a)
which calls from_numpy_array
internally based on the type of a
.
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 a 2D NumPy array. |
Scipy#
|
Returns the graph adjacency matrix as a SciPy sparse array. |
|
Returns the graph adjacency matrix as a SciPy sparse matrix. |
|
Creates a new graph from an adjacency matrix given as a SciPy sparse matrix. |
|
Creates a new graph from an adjacency matrix given as a SciPy sparse array. |
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. |