Function to make column names conventional (snake case)

In case it’s useful …

import re
def fix_headers( dframe ) :
    """ df --> df """
    # will change column names from camelCase to snake_case
    mapper_dict = {}
    for col in dframe.columns :
        mapper_dict[ col ] = re.sub( '([a-z0-9])([A-Z])', r'\1_\2', col).lower()
    dframe.rename( mapper_dict, axis=1, inplace=True )
    return dframe
4 Likes