from_pandas_adjacency#

from_pandas_adjacency(df, create_using=None)[source]#

Returns a graph from Pandas DataFrame.

The Pandas DataFrame is interpreted as an adjacency matrix for the graph.

Parameters:
dfPandas DataFrame

An adjacency matrix representation of a graph

create_usingNetworkX graph constructor, optional (default=nx.Graph)

Graph type to create. If graph instance, then cleared before populated.

Notes

For directed graphs, explicitly mention create_using=nx.DiGraph, and entry i,j of df corresponds to an edge from i to j.

If df has a single data type for each entry it will be converted to an appropriate Python data type.

If df has a user-specified compound data type the names of the data fields will be used as attribute keys in the resulting NetworkX graph.

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(G)
Graph named 'Graph from pandas adjacency matrix' with 2 nodes and 3 edges