DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk3 min

Turn Repeated Measurement Columns into Rows with pandas.wide_to_long

Reshape systematic repeated measurement columns into long form with pandas wide_to_long, understand its key arguments, and know when melt offers more control.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use pandas.wide_to_long when a DataFrame has repeated measurement columns named with a shared stub and suffix, such as score_2022 and score_2023. It combines those columns into one score column and puts the suffix in a field such as year. The original row identifier and suffix form the result’s index.

When wide_to_long fits

The function is designed for column names that follow a consistent stub-plus-suffix pattern. For example, score_2022 and score_2023 share the stub score; the underscore separates the stub from the suffix, and the suffix identifies the year. You can transform more than one measurement group at once if they share the same suffix pattern.

As an Amazon Associate I earn from qualifying purchases.

Use the complete key for each original row as i. That may be one column, such as person_id, or a list of columns if no single field uniquely identifies a row. Other columns that are not reshaped remain in the output.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Reshape repeated measurements with a shared pattern

Suppose df contains person_id, score_2022, score_2023, hours_2022, and hours_2023. This call reshapes both measurement groups:

long = pd.wide_to_long(
    df,
    stubnames=["score", "hours"],
    i="person_id",
    j="year",
    sep="_",
)

The resulting columns include score and hours. The year-like suffix is represented by year, and the result is indexed by the original identifier and that suffix. If the DataFrame has additional non-measurement columns, they remain available in the result.

What the arguments mean

  • df: the wide-format DataFrame to reshape.
  • stubnames: one or more shared prefixes for repeated measurement columns, such as score and hours.
  • i: the column or columns that identify each original row. Use the full key for the row.
  • j: the name to give the field represented by the suffix, such as year.
  • sep: the separator between a stub and suffix in the source column names. Set it to "_" for names such as score_2022.
  • suffix: a regular expression that controls which suffixes match. The default matches numeric suffixes; use a broader expression such as r"w+" for word-like labels.

Handle numeric and word-like suffixes

With numeric suffixes such as 2022 and 2023, pandas converts the suffix field to an integer or floating-point type. If the suffixes are words, such as one and two, set suffix to a pattern that matches them; r"w+" is one option for word-like labels. Those non-numeric suffixes remain labels rather than being converted to numbers.

Choose between wide_to_long and melt

Question wide_to_long melt
Do column names follow a regular pattern? Best suited to repeated groups built from shared stubs and common suffixes. Useful when names are irregular or do not follow that structure.
How are measurement columns selected? Describe groups through stubnames and the suffix pattern. Offers explicit control over identifier and value columns.
What is the output layout? Places the suffix in the field named by j and uses the identifier and suffix as the index. Creates variable and value columns according to the chosen melt arguments.

The pandas API describes wide_to_long as “Less flexible but more user-friendly than melt.” Choose melt when explicit column selection or a less regular naming scheme matters more than the convenience of describing repeated groups by their shared pattern.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check the input before reshaping

  • Confirm that i uniquely identifies each source row; include multiple columns when needed.
  • Check that every intended measurement column follows the stub, separator, and suffix pattern you specify.
  • Use a suffix expression that matches the labels in the column names, especially when suffixes are words rather than numbers.
  • Inspect the output index and suffix type to make sure they match how you plan to use the reshaped data.

How this differs from pivoting

wide_to_long and melt unpivot data from wide to long form. pivot reshapes in the opposite direction for suitable inputs. pivot_table can also reshape in that direction and aggregate when index-and-column combinations have duplicates. See the pandas reshaping and pivot tables guide for the related operations.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Version considerations

The current pandas guide and general reference are published for pandas 3.0.6; the API page for wide_to_long is also available for pandas 2.1.4. A pandas 3.1.0 release-candidate example demonstrates non-numeric suffixes. If compatibility with a particular installation matters, check that version’s API documentation: wide_to_long API reference. The documented parameters and examples are described in the reshaping guide, the pandas 2.1.4 API reference, and the pandas 3.1.0rc0 API example.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.