Adjust df.columns to comply with gbq requirements + lowercase

PHOTO EMBED

Sun Jan 23 2022 13:48:06 GMT+0000 (Coordinated Universal Time)

Saved by @jmbenedetto #python

def apply_bq_naming_requirements(dataframe: pd.DataFrame) -> pd.DataFrame:
    """
    Removes accents.
    Replaces spaces and `-` with `_`.
    Lowercase.

    Args:
      dataframe: Dataframe to be formatted.

    Returns:
      Pandas dataframe with columns changed.
    """

    def strip_accents(s):
        return ''.join(c for c in unicodedata.normalize('NFD', s)
                       if unicodedata.category(c) != 'Mn')

    fixed_columns = [column.replace("-", "_") for column in dataframe.columns.tolist()]
    fixed_columns = [column.replace(" ", "_") for column in fixed_columns]
    fixed_columns = [strip_accents(column) for column in fixed_columns]
    fixed_columns = [column.lower() for column in fixed_columns]
    dataframe.columns = fixed_columns
    return dataframe
content_copyCOPY