Skip to main content
`Ctrl`+`K`

# to_pandas_edgelist

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

  Returns the graph edge list as a Pandas DataFrame.

  Parameters:
    G graph

      The NetworkX graph used to construct the Pandas DataFrame.

    source str or int, optional

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

    target str or int, optional

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

    nodelist list, optional

      Use only nodes specified in nodelist

    dtype dtype, default None

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

    edge_key str 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:
    df Pandas 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