def _transform_index(index, func, level=None):
if isinstance(index, MultiIndex):<EOL><INDENT>if level is not None:<EOL><INDENT>items = [tuple(func(y) if i == level else y<EOL>for i, y in enumerate(x)) for x in index]<EOL><DEDENT>else:<EOL><INDENT>items = [tuple(func(y) for y in x) for x in index]<EOL><DEDENT>return MultiIndex.from_tuples(items, names=index.names)<EOL><DEDENT>else:<EOL><INDENT>items = [func(x) for x in index]<EOL>return Index(items, name=index.name, tupleize_cols=False)<EOL><DEDENT>
Apply function to all values found in index.

This includes transforming multiindex entries separately.
Only apply function to one level of the MultiIndex if level is specified.