Build Your First Social Network Graph in Python With an AI Coding Assistant
Short answer. A first social network graph can be built from two small tables: one listing people and another listing their relationships. Python libraries can turn those tables into a visual network, while an AI coding assistant can help explain and adapt the code. Starting with fictional data makes it easier to understand nodes, edges, direction, and ego networks before analyzing sensitive real-world relationships.
- A node represents an actor and an edge represents a recorded relationship.
- An edge list and node table provide a manageable starting structure.
- An ego network focuses on one selected actor and their immediate connections.
- Network diagrams depend on how relationships are defined and collected.
- An AI coding assistant can help generate code, but analytical choices remain the researcher's responsibility.
Start with a question and a small network
Social network analysis for beginners starts with a clear definition of what counts as a relationship. A graph is a mathematical representation containing nodes and edges. Nodes represent the actors being studied, while edges represent relationships between them. In a friendship network, an edge might mean that one person identifies another as a friend. In a collaboration network, it might mean that two people worked on the same project.
For a first exercise, consider a fictional study group with five students: Ava, Ben, Chloe, Daniel, and Elena. The question is simple: who collaborates with whom? The example includes six recorded collaboration relationships. It does not represent actual students, institutional data, or a measured social group. Its purpose is to demonstrate how a network can be represented in a consistent format.
- Ava collaborates with Ben and Chloe.
- Ben collaborates with Chloe and Daniel.
- Chloe collaborates with Elena.
- Daniel collaborates with Elena.
- Elena has no additional recorded relationships.
Prepare the node and edge tables
The node table contains one row for every actor. Give each person a unique identifier and, if needed, a group or category attribute. The edge table contains one row for each relationship. For an undirected collaboration network, the pair Ava-Ben is the same relationship as Ben-Ava, so it should appear only once. For a directed relationship, the source and target columns preserve who initiated or directed the connection.
A useful data dictionary explains every column before any analysis begins. For this example, node_id is a unique fictional identifier, name is a display label, and group is an illustrative category. The edge table uses source and target for the endpoints of each relationship and relationship_type to describe the recorded connection. This prevents an analyst from treating different kinds of ties as interchangeable.
- nodes.csv: node_id, name, group.
- edges.csv: source, target, relationship_type.
- Use stable identifiers instead of names as network keys.
- Document whether edges are directed or undirected.
- Avoid adding attributes that do not help answer the research question.
Create the graph with Python
Python is a practical language for introductory network analysis because it supports tabular data, graph construction, and visualization. NetworkX provides graph objects and common network measures. A coding assistant can help draft a short script, explain unfamiliar functions, and identify possible data-format problems. The most useful instructions specify the data structure, the desired graph type, and the expected output.
A minimal script can create the fictional graph directly. This version uses an undirected graph because collaboration is treated as a mutual relationship. The example deliberately separates node creation from edge creation, allowing every fictional student to appear even if a future dataset contains someone with no recorded connections.
A reproducible graph-building example
The following code creates five nodes and six edges. It uses a fixed list of fictional relationships, so the structure is understandable before any external files are introduced. The spring layout places connected nodes near one another for visual clarity. The positions are a drawing aid, not geographic coordinates or a measurement of social distance.
- import networkx as nx
- import matplotlib.pyplot as plt
- G = nx.Graph()
- people = ["Ava", "Ben", "Chloe", "Daniel", "Elena"]
- G.add_nodes_from(people)
- edges = [("Ava", "Ben"), ("Ava", "Chloe"), ("Ben", "Chloe"), ("Ben", "Daniel"), ("Chloe", "Elena"), ("Daniel", "Elena")]
- G.add_edges_from(edges)
- pos = nx.spring_layout(G, seed=7)
- nx.draw(G, pos, with_labels=True, node_size=1400, font_size=9)
- plt.show()
Read the graph through an ego network
An ego network is a network centered on one focal actor, called the ego, together with the actors connected to that person, called alters. Depending on the research design, an ego network can also include relationships among those alters. It is useful when the research question concerns an individual's immediate social environment rather than the entire network.
For example, Ava's one-step ego network contains Ava, Ben, and Chloe. Ben and Chloe are also connected to each other, so the example includes a relationship among the alters. That internal connection matters because it distinguishes a tightly interconnected neighborhood from one in which all relationships run through Ava. A simple way to retrieve the one-step neighborhood in NetworkX is G.subgraph(["Ava"] + list(G.neighbors("Ava"))).copy().
- Ego: Ava.
- Alters: Ben and Chloe.
- Ego-alter ties: Ava-Ben and Ava-Chloe.
- Alter-alter tie: Ben-Chloe.
- The ego network contains 3 nodes and 3 edges.
Use an AI coding assistant without outsourcing the analysis
An AI coding assistant is most useful when the task is specific. A prompt might ask for Python code that reads a node CSV and an edge CSV, constructs an undirected NetworkX graph, checks for missing endpoints, and draws a labeled network. Another prompt might ask for an explanation of the difference between degree and betweenness centrality using the fictional study group.
The analyst should still define the relationship, choose whether the graph is directed, inspect the input columns, and interpret the result. Generated code can make incorrect assumptions about duplicate edges, missing nodes, or relationship direction. Ask the assistant to explain each operation and provide a small example before adapting the script to a new dataset. This makes the code easier to maintain and the analytical decisions easier to defend.
Interpretation, limitations, and the next graph
A graph is a representation of recorded relationships, not a complete picture of social life. An absent edge can mean that a relationship does not exist, that it was not reported, or that it fell outside the study's definition. Layout algorithms can make clusters visually apparent, but visual proximity alone does not prove that people are socially close. A degree count indicates the number of recorded neighbors, not influence, popularity, or relationship quality.
Before applying the workflow to real data, document the sampling method, observation period, tie definition, missing-data rules, and privacy protections. For a classroom exercise, the fictional five-person graph is enough to practice graph construction and ego network analysis. The next step is to save the node and edge tables as CSV files, replace the hard-coded lists with file-reading code, and reproduce the same diagram from those files.
Questions readers ask
Which Python library is suitable for a first network graph?
NetworkX is a common starting point because it supports graph construction, neighborhood queries, and network measures. Matplotlib can provide a basic visual representation.
What is the difference between a node and an edge?
A node represents an actor or entity. An edge represents a relationship between two nodes, with direction and attributes depending on the research design.
How many people are needed for an ego network?
There is no universal minimum. An ego network can contain one focal actor and a small number of alters, provided the relevant relationships are defined and recorded.
What should an AI coding assistant be asked to explain?
Ask it to explain graph type, data cleaning, node and edge creation, missing endpoints, and the meaning of each network measure. Explanations are especially useful when code contains unfamiliar methods or assumptions.
When should a beginner move beyond an undirected graph?
Move to a directed graph when the relationship has a meaningful direction, such as following, sending a message, or nominating another person. Preserve that direction in both the data and interpretation.