Home > Blockchain >  How do I loop over pandas Data frame to create adjacency list?
How do I loop over pandas Data frame to create adjacency list?

Time:12-13

I have functions that create an adjacency list:

adj_list = {}
list1 = []
  
def add_node(node):
  if node not in list1:
    list1.append(node)
  else:
    print(f'Node {node} already added')

def add_edge(node, target,weight):
  temp= []
  if node in list1 and target in list1:
    temp.append(target,weight)
    adj_list[node] = temp

  elif node in adj_list:
    temp.extend(adj_list[node])
    temp.append(target,weight)
    adj_list[node] = temp 
  else:
    print('Node does not exit') 

but I can only add nodes and edges manually like this:

add_node(0)
add_node(1)

add_edge(0,1,2)
add_edge(1,2,2)

But I have a relatively large graph, and I can't add them all by hand, so I put sample graph data in a data frame:

node target weight
0 1 2
1 2 2
2 3 4
3 0 5
3 4 3
4 0 1

Looping over df['node'] works:

for i in df['node']:
  add_node(i)

but I can't find a way to do the same for add_edges

So what I am trying to build is a function that takes a data frame and returns a graph, like this:

# graph
0  --->  [[1, 2]]
1  --->  [[2, 2]]
2  --->  [[3, 4]]
3  --->  [[0, 5], [4, 3]]
4  --->  [[0, 1]]

# adj_list
{0: [[1, 2]], 1: [[2, 2]], 2: [[3, 4]], 3: [[0, 5], [4, 3]], 4: [[0, 1]]}

Also, I know about libraries like networkx but I need to implement this from scratch.

CodePudding user response:

You can easily do the same for add_edge, by looping over each row of the dataframe using iterrows:

for idx, row in df.iterrows():
    add_edge(row.node, row.target, row.weight)
  • Related