import collections


def _remane_duplicate_columns(names):
    """Rename a column when it is duplicated."""
    items = collections.Counter(names).items()
    dup = []
    for item, count in items:
        if count > 2:
            raise ValueError(
                f"Cannot handle more than two duplicated columns. Found {count} for {item}.",
            )
        if count > 1:
            dup.append(item)

    second_occurrences = [names[::-1].index(item) for item in dup]
    for idx in second_occurrences:
        idx += 1
        names[idx] = f"{names[idx]}_"
    return names