Building graphs from data and files

NetworkX · Essentials · 12 min

Real graphs come from tables and files, and you often need standard test graphs to try ideas on.

Most graphs start as a table of edges. Ways to load them:

  • From a list: nx.Graph([(u, v), ...]) or nx.DiGraph(...), with attributes as a third dict element, (u, v, {"weight": 3}).
  • From pandas: nx.from_pandas_edgelist(df, source="src", target="dst", edge_attr=["weight"], create_using=nx.DiGraph), and back with nx.to_pandas_edgelist(G).
  • From a dict of lists or dict of dicts: nx.Graph({"a": ["b", "c"]}).

File formats in nx.readwrite:

  • Edge lists: nx.read_edgelist(path, nodetype=int, data=[("weight", float)]) and nx.write_edgelist. Simple, but node attributes are lost and node IDs come back as strings unless you pass nodetype.
  • GraphML (read_graphml, write_graphml): XML that keeps node and edge attributes and is readable by Gephi and Cytoscape.
  • GML, GEXF, Pajek, adjacency lists.
  • JSON for web visualization: nx.node_link_data(G) and nx.node_link_graph(data).

Generators create standard graphs: nx.complete_graph(5), nx.path_graph(4), nx.grid_2d_graph(3, 3), random models like nx.erdos_renyi_graph(n, p, seed=1) and nx.barabasi_albert_graph(n, m, seed=1), and classic datasets like nx.karate_club_graph(). Pass seed= for reproducible random graphs.

Pickle is not recommended for long-term storage; prefer a standard format.

Example

import networkx as nx
import pandas as pd

edges = pd.DataFrame({"src": ["a", "b", "c"], "dst": ["b", "c", "a"],
                      "weight": [1.5, 2.0, 0.5]})
G = nx.from_pandas_edgelist(edges, "src", "dst", edge_attr="weight",
                            create_using=nx.DiGraph)
print(G, G["c"]["a"]["weight"])          # DiGraph with 3 nodes and 3 edges 0.5

nx.write_edgelist(G, "g.edgelist", data=["weight"])
H = nx.read_edgelist("g.edgelist", create_using=nx.DiGraph,
                     data=[("weight", float)])
print(sorted(H.edges(data="weight")))    # [('a', 'b', 1.5), ('b', 'c', 2.0), ('c', 'a', 0.5)]

nx.write_graphml(G, "g.graphml")
print(nx.read_graphml("g.graphml").number_of_edges())   # 3

K = nx.karate_club_graph()
print(K.number_of_nodes(), K.number_of_edges())          # 34 78

Remember

  • from_pandas_edgelist turns edge tables into graphs
  • GraphML and GEXF keep attributes; edge lists are minimal
  • Generators give standard and random test graphs; pass seed=

Watch out for

  • Reading an edge list of integers without nodetype=int, so nodes '1' and 1 differ.

Check yourself

  1. You read an integer edge list with nx.read_edgelist(path) and G.has_node(1) is False. Why?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: networkx.org/documentation/stable/reference/readwrite/index.html