Customising Settings for Edges and Nodes Using Info from a DataFrame
=====================================================
In this article, we’ll explore how to customise settings for edges and nodes in a NetworkX graph using information from a pandas DataFrame. We’ll cover the basics of NetworkX and pandas, as well as some advanced techniques for visualizing networks.
Introduction to NetworkX and Pandas
NetworkX is a Python library used for creating, manipulating, and studying the structure, dynamics, and functions of complex networks. It provides an efficient way to represent networks as graphs, which can be used in various fields such as physics, biology, sociology, and more.
Pandas, on the other hand, is a powerful data manipulation and analysis library in Python. It’s particularly useful for working with structured data, such as tables or spreadsheets.
Creating a Network from a DataFrame
Let’s start by creating a sample network using NetworkX from a pandas DataFrame:
import networkx as nx
import pandas as pd
# Create a sample DataFrame
df = pd.DataFrame({
'Person1': ['Adam', 'Mary', 'Samuel', 'Lucas', 'Christopher'],
'Age': [3, 5, 24, 12, 0.9],
'Person2': ['Yao Ming', 'Adam Lebron', 'Mary Lane', 'Julie Lime', 'Matt Red'],
'Wedding': ['Green', 'Green', 'Orange', 'Yellow', 'Green']
})
# Create a NetworkX graph from the DataFrame
G = nx.from_pandas_edgelist(df, source='Person1', target='Person2')
In this example, we create a sample DataFrame df with three columns: Person1, Age, and Person2. We then use the from_pandas_edgelist() function to create a NetworkX graph G from the DataFrame. The source and target parameters specify which columns to use as the source and target nodes for each edge.
Customising Edges
One of the most common customisation options for edges in a NetworkX graph is to set their weights or sizes based on certain attributes of the nodes. In our case, we want to set the size of each edge to be proportional to the age of the corresponding node.
# Calculate the edge weights (ages)
edge_weights = df['Age']
# Set the edge weights
G.set_edge_attributes('weight', edge_weights)
# Print the updated graph
print(G.edges(data=True))
In this example, we calculate the edge weights by extracting the Age column from the DataFrame. We then use the set_edge_attributes() function to set these weights as attributes of each edge in the graph.
Customising Nodes
Another common customisation option for nodes is to set their colours or other visual attributes based on certain attributes of the nodes. In our case, we want to set the colour of each node to be the corresponding wedding colour.
# Create a list of unique wedding colours
wedding_colours = df['Wedding'].unique()
# Set the node colour map
node_colour_map = {colour: 'light' + str(colour) for colour in wedding_colours}
# Update the graph with the new node colours
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos, nodelist=collist['value'], node_color=[node_colour_map[col] for col in collist['Wedding']])
In this example, we create a list of unique wedding colours by extracting the Wedding column from the DataFrame. We then define a node colour map as a dictionary, where each key corresponds to a unique wedding colour and the value is a string indicating the lightness of the colour. Finally, we update the graph with the new node colours using the draw_networkx_nodes() function.
Advanced Techniques
There are many other advanced techniques for customising edges and nodes in NetworkX graphs. Some examples include:
- Edge filtering: Using edge filters to remove or mask certain edges based on their weights or attributes.
# Remove edges with weights above 10
G.remove_edges_from([edge for edge, weight in G.edges(data=True) if weight > 10])
- Node clustering: Clustering nodes together based on their attributes or weights using algorithms like k-means or hierarchical clustering.
from sklearn.cluster import KMeans
# Create a K-means model with 3 clusters
kmeans = KMeans(n_clusters=3)
# Fit the model to the node data
kmeans.fit(collist[['Age', 'Wedding']])
# Predict the cluster labels for each node
cluster_labels = kmeans.predict(collist[['Age', 'Wedding']])
- Visualisation: Visualising networks using libraries like Matplotlib or Plotly.
import matplotlib.pyplot as plt
# Create a figure and axis object
fig, ax = plt.subplots()
# Draw the nodes and edges on the plot
nx.draw_networkx_nodes(G, pos, nodelist=collist['value'], node_color=[node_colour_map[col] for col in collist['Wedding']])
nx.draw_networkx_edges(G, pos, width=[i['weight'] for i in G.edges(data=True)], edge_color='black')
These are just a few examples of the many techniques available for customising edges and nodes in NetworkX graphs. By applying these techniques to your own networks, you can gain valuable insights into their structure and dynamics.
Last modified on 2025-03-28