networkx.convert_matrix.from_pandas_adjacency¶
-
from_pandas_adjacency
(df, create_using=None)[source]¶ Return a graph from Pandas DataFrame.
The Pandas DataFrame is interpreted as an adjacency matrix for the graph.
Parameters: - df (Pandas DataFrame) – An adjacency matrix representation of a graph
- create_using (NetworkX graph constructor, optional (default=nx.Graph)) – Graph type to create. If graph instance, then cleared before populated.
Notes
If the numpy matrix has a single data type for each matrix entry it will be converted to an appropriate Python data type.
If the numpy matrix has a user-specified compound data type the names of the data fields will be used as attribute keys in the resulting NetworkX graph.
See also
Examples
Simple integer weights on edges:
>>> import pandas as pd >>> pd.options.display.max_columns = 20 >>> df = pd.DataFrame([[1, 1], [2, 1]]) >>> df 0 1 0 1 1 1 2 1 >>> G = nx.from_pandas_adjacency(df) >>> G.name = 'Graph from pandas adjacency matrix' >>> print(nx.info(G)) Name: Graph from pandas adjacency matrix Type: Graph Number of nodes: 2 Number of edges: 3 Average degree: 3.0000