We learn how to formulate from long to wide melt(). But wide_to_long() function, formatting is easier than melt() in some cases. Here is one example:

pop = pd.DataFrame({'country':['Monaco', 'Liechtenstein', 
'San Marino'],
'population_2016' : [38070, 37658, 33504],
'population_2017' : [38392, 37800, 33671],
'population_2018' : [38682, 37910, 33785]})

Using melt(), we can reformat the data and format it as follows:

new = pop.melt(id_vars='country')
.rename(columns={'variable': 'year',
'value': 'population'})
new['year'] = new['year'].str.replace('population_', '')

With wide_to_long(), it is much simpler to get the same result:

pd.wide_to_long(pop, stubnames='population', i='country', j='year', 

When using the function, it is good to understand these three main terms: stub name (stubnames), terminal and separator (sep). While these terms may be self-explanatory, an example may clarify them: population is the name of the stub, 2017 is a suffix and _ is a delimiter. The new end column name is passed to the parameter j and the unique ID column name is passed to the parameter i. Free reset_index(), the output would appear to be as follows if the unique identifier and terminal column are in the directory:


By default, the suffix is ​​set to numeric values. So this worked well in our previous example. But it may not work with such information:

iris = load_dataset('iris').head()

This time, there are two stub names: curtain and petal. We care about both on the list stubnames when editing. Add-ons (i.e., length and width) are no longer numeric, so we need to determine this pattern using ordinary look in suffix Argument.

pd.wide_to_long(iris.reset_index(), stubnames=['sepal', 'petal'], 
i='index', j='Measurement', sep='_', suffix='D+')

Hopefully these two examples have shown how useful wide_to_long() can be when used in the correct settings.

Voila! These were 3 easy ways to reformat panda data! Here is their official documentation if you want to learn more: pd.melt(), pd.pivot() and pd.wide_to_long().


Please enter your comment!
Please enter your name here