Created
August 6, 2021 16:37
-
-
Save ki-chi/717ef19f4dacdf6b8b33b4968c5515e5 to your computer and use it in GitHub Desktop.
Melt hierarchal columns of pandas dataframe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def melt_columns(df: pd.DataFrame, sep: str="_") -> pd.DataFrame: | |
"""与えられたデータフレームの列がMultiIndexの場合にそれを非階層なIndexにして返す. | |
Arguments: | |
df (pd.DataFrame) : 列を非階層にしたいデータフレーム. | |
sep (str) : 新たな列名を作るときにセパレーターとして使われる文字列. デフォルトでは"_". | |
Return: | |
pd.DataFrame : 列名が非階層なIndexとなっているデータフレーム | |
""" | |
if not isinstance(df.columns, pd.MultiIndex): | |
return df | |
def get_level(multi_col, i): | |
num_levels = len(multi_col.names) | |
indices = list(range(num_levels)) | |
indices.remove(i) | |
return multi_col.droplevel(indices) | |
multi_col = df.columns.remove_unused_levels() | |
num_levels = len(multi_col.names) | |
separated_columns = [get_level(multi_col, i) for i in range(num_levels)] | |
melted_colnames = [] | |
for cols in zip(*separated_columns): | |
cols_without_whitespace = [colname for colname in cols if colname != ""] | |
melted_col = sep.join(cols_without_whitespace) | |
melted_colnames.append(melted_col) | |
return df.set_axis(melted_colnames, axis="columns") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment