flattening multi index dataframe

Solutions on MaxInterview for flattening multi index dataframe by the best coders in the world

showing results for - "flattening multi index dataframe"
Gianluca
22 Jan 2016
1def tidify(df):
2    if isinstance(df.index, pd.core.index.MultiIndex):
3        df = df.copy()
4        new_colnames = []
5        for v in df.columns:
6            new_colnames.append('_'.join(v))
7        df.columns = new_colnames
8        df.reset_index(inplace=True)
9        return df
10    else:
11        df = df.copy()
12        df.reset_index(inplace=True)
13        return df
14