Pivot long to wide transformation dict
Transform a list of dictionaries from long format to wide format by pivoting on a key column and aggregating values, using pure Python.
Python code
45 linesdef pivot_long_to_wide(rows, key_col, value_col, id_cols=None):
"""
Convert long-format data (list of dicts) to wide format.
Args:
rows: List of dicts in long format
key_col: Column name to pivot on (becomes new column headers)
value_col: Column name whose values become the cell values
id_cols: List of columns to keep as identifiers (defaults to all except key/value cols)
Returns:
List of dicts in wide format
"""
if id_cols is None:
id_cols = [k for k in rows[0] if k not in (key_col, value_col)]
# Group rows by identifier values
grouped = {}
for row in rows:
key = tuple(row[c] for c in id_cols)
if key not in grouped:
grouped[key] = {}
grouped[key][row[key_col]] = row[value_col]
# Build wide-format rows
result = []
for key_values, pivot_map in grouped.items():
wide_row = dict(zip(id_cols, key_values))
wide_row.update(pivot_map)
result.append(wide_row)
return result
if __name__ == "__main__":
data = [
{"month": "Jan", "region": "North", "sales": 100},
{"month": "Jan", "region": "South", "sales": 150},
{"month": "Feb", "region": "North", "sales": 120},
{"month": "Feb", "region": "South", "sales": 180},
]
result = pivot_long_to_wide(data, key_col="month", value_col="sales", id_cols=["region"])
for row in result:
print(row)
Output
{'region': 'North', 'Jan': 100, 'Feb': 120}
{'region': 'South', 'Jan': 150, 'Feb': 180}
How it works
The function groups rows by the identifier columns, building a dictionary where each identifier tuple maps to a pivot map. Then it reconstructs each wide row by merging the identifier values with the pivot map. This avoids external dependencies while giving full control over the transformation. The order of rows follows the insertion order of the dictionary, which preserves the order of first appearance.
Common mistakes
- Assuming the same key appears exactly once per identifier; duplicates overwrite values silently
- Not handling empty rows list; crashes on rows[0] if id_cols is None
- Mixing types across key column values, causing inconsistent column names
Variations
- Use pandas.DataFrame.pivot_table for large datasets with built-in aggregation
- Use collections.defaultdict to simplify the grouping step
Real-world use cases
- Converting time-series sensor readings from a long table into wide format for dashboarding
- Reshaping survey responses so each respondent becomes one row with questions as columns
- Preparing daily sales totals by product for a retail reporting pipeline
Sponsored
More from Data pipelines & processing
- Add a UUID Surrogate Key to Each Row in a CSV with Python easy
- Attach Source File Metadata to Records in Python easy
- Build a Python Utility That Detects Duplicate Records Across Multiple Excel Sheets medium
- Check Null Rate Threshold in PySpark DataFrame medium
- Count Records Processed per Category in Python easy
- Create Data Helper Functions in Python for Beginners easy
Keep learning
Related tutorials and quizzes for this topic.