melt()
Swipe to show menu
When working with data in pandas, you often encounter DataFrames in a wide format, where each variable is in its own column. However, for many analytical tasks β such as plotting, aggregation, or working with certain libraries β you need your data in a long format, where variables are stored in a single column and their values in another. The process of converting wide-format data to long-format is called melting. The melt() function in pandas is a powerful tool for this transformation, making it easier to tidy your data and prepare it for further analysis.
12345678910111213141516171819import pandas as pd # Sample wide-format DataFrame df = pd.DataFrame({ "Name": ["Alice", "Bob"], "Math": [85, 90], "Science": [92, 88] }) # Melt the DataFrame to long format long_df = pd.melt( df, id_vars=["Name"], value_vars=["Math", "Science"], var_name="Subject", value_name="Score" ) print(long_df)
The melt() function offers several parameters to control how your DataFrame is reshaped:
id_vars: columns to keep fixed (not unpivoted); these often serve as identifiers, like names or IDs;value_vars: columns to unpivot, which become variable entries in the new long-format DataFrame;var_name: name for the new column that will hold the names of the unpivoted columns; if not specified, defaults to "variable";value_name: name for the new column that will hold the values from the unpivoted columns; if not specified, defaults to "value".
By carefully selecting these parameters, you can reshape your data exactly as needed for your analysis tasks.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat