This documents the development version of NetworkX. Documentation for the current release can be found here.


from_pandas_edgelist(df, source='source', target='target', edge_attr=None, create_using=None, edge_key=None)[source]

Returns a graph from Pandas DataFrame containing an edge list.

The Pandas DataFrame should contain at least two columns of node names and zero or more columns of edge attributes. Each row will be processed as one edge instance.

Note: This function iterates over DataFrame.values, which is not guaranteed to retain the data type across columns in the row. This is only a problem if your row is entirely numeric and a mix of ints and floats. In that case, all values will be returned as floats. See the DataFrame.iterrows documentation for an example.

  • df (Pandas DataFrame) – An edge list representation of a graph

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

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

  • edge_attr (str or int, iterable, True, or None) – A valid column name (str or int) or iterable of column names that are used to retrieve items and add them to the graph as edge attributes. If True, all of the remaining columns will be added. If None, no edge attributes are added to the graph.

  • create_using (NetworkX graph constructor, optional (default=nx.Graph)) – Graph type to create. If graph instance, then cleared before populated.

  • edge_key (str or None, optional (default=None)) – A valid column name for the edge keys (for a MultiGraph). The values in this column are used for the edge keys when adding edges if create_using is a multigraph.


Simple integer weights on edges:

>>> import pandas as pd
>>> pd.options.display.max_columns = 20
>>> import numpy as np
>>> rng = np.random.RandomState(seed=5)
>>> ints = rng.randint(1, 11, size=(3,2))
>>> a = ['A', 'B', 'C']
>>> b = ['D', 'A', 'E']
>>> df = pd.DataFrame(ints, columns=['weight', 'cost'])
>>> df[0] = a
>>> df['b'] = b
>>> df[['weight', 'cost', 0, 'b']]
   weight  cost  0  b
0       4     7  A  D
1       7     1  B  A
2      10     9  C  E
>>> G = nx.from_pandas_edgelist(df, 0, 'b', ['weight', 'cost'])
>>> G['E']['C']['weight']
>>> G['E']['C']['cost']
>>> edges = pd.DataFrame({'source': [0, 1, 2],
...                       'target': [2, 2, 3],
...                       'weight': [3, 4, 5],
...                       'color': ['red', 'blue', 'blue']})
>>> G = nx.from_pandas_edgelist(edges, edge_attr=True)
>>> G[0][2]['color']

Build multigraph with custom keys:

>>> edges = pd.DataFrame({'source': [0, 1, 2, 0],
...                       'target': [2, 2, 3, 2],
...                       'my_edge_key': ['A', 'B', 'C', 'D'],
...                       'weight': [3, 4, 5, 6],
...                       'color': ['red', 'blue', 'blue', 'blue']})
>>> G = nx.from_pandas_edgelist(edges,                                     edge_key='my_edge_key',                                     edge_attr=['weight', 'color'],                                     create_using=nx.MultiGraph())
>>> G[0][2]
AtlasView({'A': {'weight': 3, 'color': 'red'}, 'D': {'weight': 6, 'color': 'blue'}})