Answers for "Flattening Multi index dataframe"

0

Flattening Multi index dataframe

def tidify(df):
    if isinstance(df.index, pd.core.index.MultiIndex):
        df = df.copy()
        new_colnames = []
        for v in df.columns:
            new_colnames.append('_'.join(v))
        df.columns = new_colnames
        df.reset_index(inplace=True)
        return df
    else:
        df = df.copy()
        df.reset_index(inplace=True)
        return df
Posted by: Guest on August-09-2021

Code answers related to "Flattening Multi index dataframe"

Python Answers by Framework

Browse Popular Code Answers by Language