Reshaping with pd.wide_to_long
So far, we have encountered two very viable ways of converting data from wide to long format, whether it be through the use of the pd.DataFrame.stack
method, introduced in our Reshaping with pd.DataFrame.stack and pd.DataFrame.unstack recipe, or through the use of the pd.DataFrame.melt
, as we saw in the Reshaping with pd.DataFrame.melt recipe.
If those aren’t enough, pandas offers the pd.wide_to_long
function, which can help with that conversion given that your columns follow a particular naming pattern, as we will see in this recipe.
How to do it
Let’s assume we have the following pd.DataFrame
, where we have one id
variable of widget
and four columns representing sales from a business quarter. Each column of sales begins with "quarter_"
:
df = pd.DataFrame([
["Widget 1", 1, 2, 4, 8],
["Widget 2", 16, 32, 64, 128],
], columns=["widget", "quarter_1", "quarter_2...