Pandas データクレンジング
マルチカラム
code:python
def multi_columns(df,sep='_')->pd.DataFrame:
"""
df: pd.DataFrame
sep: str
return: pd.DataFrame
This function takes a pandas dataframe with
two row of headers and combines them into one
row of headers with a separator between them.
"""
# replace 'Unnamed: \d+' to ''
col_df = df.iloc0.reset_index().copy() col_df'index' = col_df'index'.replace('Unnamed: \d+', np.nan, regex=True).fillna(method='ffill').replace(np.nan, '') # drop first row
df = df.drop(0).reset_index(drop=True)
return df