Transform 1‑D Data to 2‑D Tables with Pandas Pivot_table and Unstack
This article demonstrates how to reshape a one‑dimensional dataset into a two‑dimensional table using pandas, covering the data requirements, import steps, and two practical methods—pivot_table with a custom aggregation function and the unstack technique—complete with code snippets and visual results.
Rescue pandas Plan (1) – Converting a One‑Dimensional Array to a Two‑Dimensional Array
Data requirement
Requirement breakdown
Data import
Requirement processing
Method 1: pivot_table
Method 2: unstack
Summary
Many users avoid pandas for data manipulation, preferring other libraries. This series aims to show pandas’ power and encourage its adoption.
Data Requirement
Transform the left‑hand data (shown in the red box) into the right‑hand format.
For small datasets, simple operations may suffice, but larger datasets benefit from pivot_table.
Requirement Breakdown
All values in tag_name become new column names, while period and simei should remain unique identifiers.
Data Import
First, import the data:
Requirement Processing
Method 1: pivot_table
Using pivot_table directly raises a non‑numeric data error because the default aggfunc is mean. Define a custom function to handle strings:
def eval_func(x):
return x
df = pd.pivot_table(df, values='tag_value2', index=['period', 'simei'], columns=['tag_name'], aggfunc=eval_func).reset_index()
df.columns = df.columns.valuesAfter resetting the index and renaming columns, the desired format is achieved.
Method 2: unstack
The same result can be achieved with unstack:
df = df.set_index(['period', 'simei', 'tag_name']).unstack('tag_name')
df.columns = df.columns.get_level_values(1).values
df.reset_index()Set the target columns as index, then unstack tag_name to turn it into column headers. After flattening multi‑level columns and resetting the index, the final table matches the requirement.
Summary
For large‑scale data processing, pandas remains a powerful tool. This example showcases only a fraction of its capabilities; readers are encouraged to explore further and share alternative solutions.
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.
