<jupyter_start><jupyter_text>Reshape dataframe using stack/unstack<jupyter_code>import pandas as pd
df = pd.read_excel("stocks.xlsx",header=[0,1])
df
df.stack()
df.stack(level=0)
df_stacked=df.stack()
df_stacked
df_stacked.unstack()<jupyter_output><empty_output><jupyter_text>3 levels of column headers<jupyter_code>df2 = pd.read_excel("stocks_3_levels.xlsx",header=[0,1,2])
df2
df2.stack()
df2.stack(level=0)
df2.stack(level=1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15065502183406113, 'fraction_numerical': 0.03275109170305677, 'mean_word_length': 11.078947368421053, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '579748', 'license': 'MIT', 'n_tokens_mistral': 205, 'n_tokens_neox': 201, 'n_words': 24, 'path': "Program's_Contributed_By_Contributors/AI-Summer-Course/py-master/pandas/12_stack/12_pandas_stack.ipynb", 'repo_name': 'siddharthdeo99/Hacktoberfest2k21', 'size': '458'}