How to reshape a networkx graph in Python?

NetworkX graphs can be reshaped by modifying the edge relationships between nodes. You can create graphs from Pandas DataFrames and reshape them by adjusting the edge list data.

Creating a Basic Graph

First, let's create a simple graph from a DataFrame ?

import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt

# Create DataFrame with edge relationships
df = pd.DataFrame({
    'from': ['A', 'B', 'C', 'A'], 
    'to': ['D', 'A', 'E', 'C']
})

print("Original edges:")
print(df)

# Create graph from DataFrame
G = nx.from_pandas_edgelist(df, 'from', 'to')
print(f"\nNodes: {list(G.nodes())}")
print(f"Edges: {list(G.edges())}")
Original edges:
  from to
0    A  D
1    B  A
2    C  E
3    A  C

Nodes: ['A', 'D', 'B', 'E', 'C']
Edges: [('A', 'D'), ('A', 'C'), ('D', 'B'), ('C', 'E')]

Reshaping by Adding Edges

You can reshape the graph by adding more connections ?

import pandas as pd
import networkx as nx

# Original graph
df_original = pd.DataFrame({
    'from': ['A', 'B', 'C'], 
    'to': ['B', 'C', 'A']
})

# Reshaped graph with additional edges
df_reshaped = pd.DataFrame({
    'from': ['A', 'B', 'C', 'A', 'B'], 
    'to': ['B', 'C', 'A', 'D', 'D']
})

G_original = nx.from_pandas_edgelist(df_original, 'from', 'to')
G_reshaped = nx.from_pandas_edgelist(df_reshaped, 'from', 'to')

print("Original graph:")
print(f"Nodes: {list(G_original.nodes())}")
print(f"Edges: {list(G_original.edges())}")

print("\nReshaped graph:")
print(f"Nodes: {list(G_reshaped.nodes())}")
print(f"Edges: {list(G_reshaped.edges())}")
Original graph:
Nodes: ['A', 'B', 'C']
Edges: [('A', 'B'), ('B', 'C'), ('C', 'A')]

Reshaped graph:
Nodes: ['A', 'B', 'C', 'D']
Edges: [('A', 'B'), ('A', 'D'), ('B', 'C'), ('B', 'D'), ('C', 'A')]

Reshaping by Removing Edges

You can also reshape by filtering the DataFrame to remove connections ?

import pandas as pd
import networkx as nx

# Start with a complex graph
df_full = pd.DataFrame({
    'from': ['A', 'B', 'C', 'D', 'A', 'C'], 
    'to': ['B', 'C', 'D', 'A', 'D', 'B']
})

# Reshape by filtering out certain connections
df_filtered = df_full[df_full['from'] != 'D']  # Remove edges starting from D

G_full = nx.from_pandas_edgelist(df_full, 'from', 'to')
G_filtered = nx.from_pandas_edgelist(df_filtered, 'from', 'to')

print("Full graph:")
print(f"Edges: {list(G_full.edges())}")

print("\nFiltered graph:")
print(f"Edges: {list(G_filtered.edges())}")
Full graph:
Edges: [('A', 'B'), ('A', 'D'), ('B', 'C'), ('C', 'D'), ('C', 'B'), ('D', 'A')]

Filtered graph:
Edges: [('A', 'B'), ('A', 'D'), ('B', 'C'), ('C', 'D'), ('C', 'B')]

Dynamic Graph Reshaping

You can dynamically modify graphs by adding or removing edges programmatically ?

import pandas as pd
import networkx as nx

# Start with basic graph
df = pd.DataFrame({
    'from': ['A', 'B'], 
    'to': ['B', 'C']
})

G = nx.from_pandas_edgelist(df, 'from', 'to')
print("Initial graph edges:", list(G.edges()))

# Add edges programmatically
G.add_edge('C', 'D')
G.add_edge('D', 'A')
print("After adding edges:", list(G.edges()))

# Remove an edge
G.remove_edge('B', 'C')
print("After removing B-C:", list(G.edges()))

# Add multiple edges at once
new_edges = [('A', 'E'), ('E', 'F')]
G.add_edges_from(new_edges)
print("Final graph edges:", list(G.edges()))
Initial graph edges: [('A', 'B'), ('B', 'C')]
After adding edges: [('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'A')]
After removing B-C: [('A', 'B'), ('C', 'D'), ('D', 'A')]
Final graph edges: [('A', 'B'), ('A', 'E'), ('C', 'D'), ('D', 'A'), ('E', 'F')]

Conclusion

You can reshape NetworkX graphs by modifying the DataFrame before creating the graph, or by using NetworkX methods like add_edge() and remove_edge(). The DataFrame approach is useful for bulk changes, while direct graph modification works well for dynamic updates.

Updated on: 2026-03-25T19:55:54+05:30

466 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements