code: def _get_target_nearest(self):
          
          reps_query = """
               SELECT DISTINCT ON(g2.cartodb_id)
                   g1.cartodb_id As origin_id,
                   g2.the_geom,
                   g2.cartodb_id + {maxorigin} as cartodb_id,
                   g2.the_geom_webmercator
               FROM {origin_table} As g1, {target_table} As g2
               ORDER BY g2.cartodb_id, g1.the_geom <-> g2.the_geom
          """.format(
              maxorigin=self.origins.index.max(),
              origin_table=self.origin_table,
              target_table=self.target_table
          )
  
          nearest_reps = self.context.query(
              reps_query,
              decode_geom=True
          )
  
          nearest_reps = gpd.GeoDataFrame(nearest_reps, geometry='geometry')
          init_labels = nearest_reps['origin_id'].values
  
          # update with new information
          self.targets['labels'] = init_labels
          logging.info('nearest targets retrieved')
  
          return nearest_reps
docs: Get nearest target for each origin