code: def aggregate_multi_graph(sub_network):
      
  
      network = sub_network.network
  
      count = 0
      seen = []
      graph = sub_network.graph()
      for u,v in graph.edges():
          if (u,v) in seen:
              continue
          line_objs = list(graph.adj[u][v].keys())
          if len(line_objs) > 1:
              lines = network.lines.loc[[l[1] for l in line_objs]]
              aggregated = {}
  
              attr_inv = ["x","r"]
              attr_sum = ["s_nom","b","g","s_nom_max","s_nom_min"]
              attr_mean = ["capital_cost","length","terrain_factor"]
  
              for attr in attr_inv:
                  aggregated[attr] = 1./(1./lines[attr]).sum()
  
              for attr in attr_sum:
                  aggregated[attr] = lines[attr].sum()
  
              for attr in attr_mean:
                  aggregated[attr] = lines[attr].mean()
  
              count += len(line_objs) - 1
  
              #remove all but first line
              for line in line_objs[1:]:
                  network.remove("Line",line[1])
  
              rep = line_objs[0]
  
              for key,value in aggregated.items():
                  setattr(rep,key,value)
  
              seen.append((u,v))
  
      logger.info("Removed %d excess lines from sub-network %s and replaced with aggregated lines", count,sub_network.name)
docs: Aggregate branches between same buses and replace with a single
  branch with aggregated properties (e.g. s_nom is summed, length is
  averaged).