code: def find_slack_bus(sub_network):
      
  
      gens = sub_network.generators()
  
      if len(gens) == 0:
          logger.warning("No generators in sub-network {}, better hope power is already balanced".format(sub_network.name))
          sub_network.slack_generator = None
          sub_network.slack_bus = sub_network.buses_i()[0]
  
      else:
  
          slacks = gens[gens.control == "Slack"].index
  
          if len(slacks) == 0:
              sub_network.slack_generator = gens.index[0]
              sub_network.network.generators.loc[sub_network.slack_generator,"control"] = "Slack"
              logger.debug("No slack generator found in sub-network {}, using {} as the slack generator".format(sub_network.name, sub_network.slack_generator))
  
          elif len(slacks) == 1:
              sub_network.slack_generator = slacks[0]
          else:
              sub_network.slack_generator = slacks[0]
              sub_network.network.generators.loc[slacks[1:],"control"] = "PV"
              logger.debug("More than one slack generator found in sub-network {}, using {} as the slack generator".format(sub_network.name, sub_network.slack_generator))
  
          sub_network.slack_bus = gens.bus[sub_network.slack_generator]
  
      #also put it into the dataframe
      sub_network.network.sub_networks.at[sub_network.name,"slack_bus"] = sub_network.slack_bus
  
      logger.info("Slack bus for sub-network {} is {}".format(sub_network.name, sub_network.slack_bus))
docs: Find the slack bus in a connected sub-network.