Home » Pandas Dataframe » Merge Nodes and Attributes from Different Datasets in Pandas

Merge Nodes and Attributes from Different Datasets in Pandas

In this tutorial, you will learn how to merge nodes and attributes from different datasets in Pandas.

Suppose you have the first dataset as below:

Node     Edge
A         B
A         D
B         N
B         A
B         X
S         C

And the second dataset has unique nodes and their properties as shown below:

Node    Attribute
A           -1
B           0
C           -1.5
D           1
...
N           1
... 
X           0
Y           -1.5
W          -1.5
Z           1

Now in the below example, we will see how we can merge the second dataset nodes and attributes to the first dataset.

Example: Merge Nodes and Attributes from Second Dataset in Pandas

The following Python code will create a mapping from Node to color by set_index to Node, and map the current attribute numerical value into a color:

import networkx as nx
import pandas as pd

edges_df = pd.DataFrame({
    'Node': ['A', 'A', 'B', 'B', 'B', 'S'],
    'Edge': ['B', 'D', 'N', 'A', 'X', 'C']
})

# Abridged but contains values for all nodes in `edges_df`
attributes_df = pd.DataFrame({
    'Node': ['A', 'B', 'C', 'D', 'N', 'S', 'X'],
    'Attribute': [-1, 0, -1.5, 1, 1, 1.5, 0]
})

mapper = {-1.5: 'grey', -1: 'red', 0: 'orange', 1: 'yellow', 1.5: 'green'}
colour_map = attributes_df.set_index('Node')['Attribute'].map(mapper)

Color Map

Node
A       red
B    orange
C      grey
D    yellow
N    yellow
S     green
X    orange
Name: Attribute, dtype: object

See also:

Reference: