def set_index(container, key):
    """
    Set the index of a container to a key, either a name or an
    integer index. If the columns of a container include an
    integer name, the position takes precedence over the
    name. That is, if a table contains columns named [1, 2, 0, 3],
    then `set_index(container, 0)` will result in a table
    with columns [2, 0, 3].

    If the Index already has an index, then the index is first reset.

    :param container, Container: Container whose index is to be
        assigned.
    :param key, str or int: Key, either by name, regex, or by position.
    :return: Copy of container with the Index set according to key.
    """
    df = container
    if container.index.name is not None:
        df = container.reset_index()
    try:
        key = df.columns[key]
    except KeyError:
        pass
    if key not in df.columns:
        raise KeyError(f"{key} was not found in the container/table.")
    return df.set_index(key)