Exploring Pandas wide_to_long() function (5 examples)

Updated: February 23, 2024 By: Guest Contributor Post a comment


Pandas is a powerful tool for data analysis and manipulation in Python, providing flexible data structures and functions to work with structured data sets. Among its versatile functions, wide_to_long() is particularly useful for reshaping DataFrames from wide to long format, which is a common requirement for many data analysis tasks and machine learning models. This tutorial will take you through five progressively complex examples of using the wide_to_long() function, offering insights into its powerful features and how to leverage them for your data manipulation needs.


  • Basic knowledge of Python.
  • Familiarity with pandas library.
  • An installed Python environment with pandas installed (you can install pandas using pip install pandas if you haven’t done so).

Example 1: Basic Transformation

Let’s start with a basic example where we have a DataFrame with multiple columns for measures taken at different times. Our goal is to reshape this DataFrame into a longer format.

import pandas as pd

df = pd.DataFrame({
    'ID': [1, 2],
    'MeasureA_Time1': [100, 200],
    'MeasureA_Time2': [150, 250],
    'MeasureB_Time1': [50, 60],
    'MeasureB_Time2': [55, 65]

print('Original DataFrame:')

long_df = pd.wide_to_long(df, stubnames=['MeasureA', 'MeasureB'], i='ID', j='Time').reset_index()
print('\nTransformed DataFrame:')

This transformation creates a long format DataFrame where each row represents a time point measurement for each ID, effectively doubling the number of records while maintaining data integrity.

Example 2: Multiple Stubs

Now, let’s complicate things a bit by having multiple measurement types across different times, all of which need to be included in the transformation.

long_df = pd.wide_to_long(df, stubnames=['MeasureA', 'MeasureB'], i=['ID'], j='Time', sep='_').reset_index()
print('\nTransformed DataFrame:')

Notice the use of sep='_' argument which correctly identifies the separator between the measurement name and time point in the column names, ensuring a smoother transformation.

Example 3: Handling Multiple Indexes

In this example, we explore how to handle situations with multiple indexes. Suppose your DataFrame has multiple levels of indexing and you want to bring it into a long format without losing the hierarchical structure.

df.set_index(['ID', 'Group'], inplace=True)
long_df = pd.wide_to_long(df, stubnames=['MeasureA', 'MeasureB'], i=['ID', 'Group'], j='Time').reset_index()
print('\nTransformed DataFrame:')

By specifying a list of columns for the i parameter, wide_to_long() adapts to data structures with more complex indices, offering flexibility in handling a variety of data formats.

Example 4: Complex Data Structures

Moving on to more complex data structures, let’s say our DataFrame includes columns for multiple measures across several times, and we also have demographic information that we want to retain across the reshaping process.

df['Gender'] = ['Male', 'Female']
df['Age'] = [25, 30]
long_df = pd.wide_to_long(df, stubnames=['MeasureA', 'MeasureB'], i=['ID', 'Gender', 'Age'], j='Time', suffix='\d+').reset_index()
print('\nTransformed DataFrame:')

The suffix='\d+' argument indicates that the time point information is strictly numerical, thereby fine-tuning the pattern matching for column names during the transformation process.

Example 5: Including Additional Data

For our final example, let’s incorporate additional columns that should remain unaltered during the reshaping process but still included in the final DataFrame.

df['Location'] = ['NY', 'CA']
long_df = pd.wide_to_long(df, stubnames=['MeasureA', 'MeasureB'], i=['ID'], j='Time', suffix='\d+', sep='_').reset_index()
print('\nTransformed DataFrame:')

This demonstrates the wide_to_long() function’s capability to accommodate additional columns seamlessly into the newly structured DataFrame, thereby enhancing its utility for comprehensive data analysis tasks.


In conclusion, the wide_to_long() function in pandas offers a robust solution for transforming wide-format DataFrames into a more analysis-friendly long format. Through the examples discussed, we’ve seen how it can be applied to various data structures and requirements, proving its versatility and power in data manipulation. With a good understanding of wide_to_long(), you can effectively restructure your data to fit analytical models and reporting needs, opening new vistas for in-depth analysis.