How to Reshape Complex Excel Tables with Pandas Pivot_table in Python
This article walks through a pandas‑based solution for transforming an Excel sheet that mixes status, ID and name columns into a clean, pivoted layout where each status and ID becomes its own column aligned with the corresponding name, complete with code and visual results.
In this article, the author addresses a pandas question from a Python automation group: how to reshape a table containing status, ID, and name columns into a format where each status and ID appears as separate columns aligned with names.
Introduction
The problem was posted in a WeChat group, asking for a DataFrame transformation using pandas.
Implementation
The author first tried openpyxl and Excel formulas, then presented a solution using pandas pivot_table. The code is shown below:
# Use pivot_table to restructure
df_new = pd.pivot_table(df, index='名称', columns=df.groupby('名称').cumcount().add(1), values=['状态', '编号'], aggfunc='first')
# Rename columns
df_new.columns = [f'状态{i}' if '状态' in col else f'编号{i}' for col, i in df_new.columns]
columns = df_new.columns.tolist()
columns = [columns[i::4] for i in range(len(columns)-4)]
columns = sum(columns, [])
df = df_new[columns].reset_index()
print(df)The resulting DataFrame matches the expected layout, as illustrated in the following screenshots.
The solution successfully resolved the user's issue.
Conclusion
The article demonstrates a practical pandas pivot_table technique for converting irregular Excel data into a structured format, providing the full code and visual results.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
