Feature Type
-
[X] Adding new functionality to pandas
-
[ ] Changing existing functionality in pandas
-
[ ] Removing existing functionality in pandas
Problem Description
I use melt()
and pivot()
all the time but can never remember which one makes my dataframe longer and which one makes it wider. So even though I use it on a daily to weekly level, I almost always have to look up which one is which.
For the same reason, R renamed their gather
and spread
functions to pivot_wider()
and pivot_longer()
(see also their vignette on pivoting).
Feature Description
I assume it could be as simple as adding some function aliasses (plus updating documentation):
pivot_longer = melt
pivot_wider = pivot
Alternative Solutions
I sometimes wrote little wrapper functions for my code around melt and pivot but the disadvantage of doing this is that - I have to redo it in every analysis - I can't chain the command
import pandas as pd
pivot_longer = pd.melt
pivot_wider = pd.pivot
Additional Context
I haven't contributed yet any code to pandas but would be happy to take this one up myself with some pointers where to make the changes
Comment From: MarcoGorelli
Hey Corrie,
First, I agree that the current names are confusing, I can never remember them either.
But - what should be done about that? Is pandas pivot the same as R pivot_wider
? Because if not, then renaming so that they have the exact same names might be confusing
Even without renaming, though, there's already https://pandas.pydata.org/docs/reference/api/pandas.wide_to_long.html
Comment From: samukweku
@corriebar FWIW, have a look at pivot_longer and pivot_wider from pyjanitor, which is similar to R's .
Comment From: corriebar
As far as I can see (and how I use the functions), pd.melt()
is the same as pivot_longer
and pd.pivot()
is the same as pivot_wider
.
The only difference I think is that R provides a bit more options, eg pattern matching column names. The python functions on the other hand can return MultiIndex which doesn't exist in R. In the base usage, I would say there are the same. I haven't used R in a while though, so there might be some details I'm missing.
Any other name easier to remember would also make me happy but personally I think functionality wise they are close enough to the R equivalents.
pd.wide_to_long()
is indeed easier to remember but very rarely works for me as my columns don't have a common pattern.
Comment From: samukweku
import pandas.melt as pivot_longer
is one option.
Comment From: corriebar
import pandas.melt as pivot_longer
I think this should be
from pandas.core.reshape import melt as pivot_longer
and is equivalent to
import pandas as pd
pivot_longer = pd.melt
The disadvantage of this approach is that it cannot be chained, i.e. one can only do
df_long = pivot_longer(df, ...)
but not
df_long = df.pivot_longer(....)
Comment From: samukweku
Then do a pipe :
df.pipe(pivot_longer, ...)
Comment From: corriebar
Thanks @samukweku for the tips, didn't think about using a pipe. I think these are good work arounds (the pyjanitor package as well).
Imho, they don't solve the underlying problem that the current names are confusing though. I'm gonna start using some of the proposed workarounds but think pandas could still benefit from having some clearer names for these two functions.
Comment From: phofl
General note: Please don't import from pandas.core