networkx 2.8.2 Pypi GitHub Homepage
Other Docs
ParametersBackRef
from_pandas_edgelist(df, source='source', target='target', edge_attr=None, create_using=None, edge_key=None)

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.

Parameters

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 :None:None:`True`, all of the remaining columns will be added. If :None:None:`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.

Returns a graph from Pandas DataFrame containing an edge list.

See Also

to_pandas_edgelist

Examples

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"] 10
>>> G["E"]["C"]["cost"]
9
>>> 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"] 'red'

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'}})
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

networkx.convert_matrix.from_pandas_edgelist

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /networkx/convert_matrix.py#315
type: <class 'function'>
Commit: