What if you could reshape messy tables in one line instead of hours of copying?
Why melt() for unpivoting in Pandas? - Purpose & Use Cases
Imagine you have a table with sales data for different products across several months, all in separate columns. You want to analyze sales trends over time, but the data is wide and hard to compare.
Manually copying and rearranging columns into rows is slow and boring. It's easy to make mistakes, like mixing up months or missing data. Plus, if the data updates, you have to redo everything.
The melt() function quickly turns wide tables into long tables by stacking columns into rows. This makes data tidy and ready for analysis without manual copying or errors.
new_data = [] for month in ['Jan', 'Feb', 'Mar']: for i, row in df.iterrows(): new_data.append({'Product': row['Product'], 'Month': month, 'Sales': row[month]}) new_df = pd.DataFrame(new_data)
new_df = df.melt(id_vars=['Product'], var_name='Month', value_name='Sales')
It lets you quickly reshape data to explore trends, create charts, and build models with tidy, easy-to-use tables.
A store manager can transform monthly sales columns into a single 'Month' column to easily plot sales over time and spot which months perform best.
Manual reshaping is slow and error-prone.
melt() automates turning columns into rows.
This makes data tidy and ready for analysis.