to_pandas_edgelist

to_pandas_edgelist(G, source='source', target='target', nodelist=None, dtype=None, order=None, edge_key=None)[source]

Returns the graph edge list as a Pandas DataFrame.

Parameters
Ggraph

The NetworkX graph used to construct the Pandas DataFrame.

sourcestr or int, optional

A valid column name (string or integer) for the source nodes (for the directed case).

targetstr or int, optional

A valid column name (string or integer) for the target nodes (for the directed case).

nodelistlist, optional

Use only nodes specified in nodelist

dtypedtype, default None

Use to create the DataFrame. Data type to force. Only a single dtype is allowed. If None, infer.

orderNone

An unused parameter mistakenly included in the function.

Deprecated since version 2.6: This is deprecated and will be removed in NetworkX v3.0.

edge_keystr or int or None, optional (default=None)

A valid column name (string or integer) for the edge keys (for the multigraph case). If None, edge keys are not stored in the DataFrame.

Returns
dfPandas DataFrame

Graph edge list

Examples

>>> G = nx.Graph(
...     [
...         ("A", "B", {"cost": 1, "weight": 7}),
...         ("C", "E", {"cost": 9, "weight": 10}),
...     ]
... )
>>> df = nx.to_pandas_edgelist(G, nodelist=["A", "C"])
>>> df[["source", "target", "cost", "weight"]]
  source target  cost  weight
0      A      B     1       7
1      C      E     9      10
>>> G = nx.MultiGraph([('A', 'B', {'cost': 1}), ('A', 'B', {'cost': 9})])
>>> df = nx.to_pandas_edgelist(G, nodelist=['A', 'C'], edge_key='ekey')
>>> df[['source', 'target', 'cost', 'ekey']]
  source target  cost  ekey
0      A      B     1     0
1      A      B     9     1