Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →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.
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:
#1 Best Overall
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 asscoreandhours.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 asyear.sep: the separator between a stub and suffix in the source column names. Set it to"_"for names such asscore_2022.suffix: a regular expression that controls which suffixes match. The default matches numeric suffixes; use a broader expression such asr"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.
Rank #2
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.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problemsCheck the input before reshaping
- Confirm that
iuniquely 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.
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.
Quick Recap
Best Value
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.




