w3resource

Pandas: Data Manipulation - wide_to_long() function

wide_to_long() function

Wide panel to long format. Less flexible but more user-friendly than melt.
With stubnames [‘A’, ‘B’], this function expects to find one or more group of columns with format A-suffix1, A-suffix2,…, B-suffix1, B-suffix2,… You specify what you want to call this suffix in the resulting long format with j (for example j=’year’)
Each row of these wide variables are assumed to be uniquely identified by i (can be a single column name or a list of column names)
All remaining variables in the data frame are left intact.

Syntax:

pandas.wide_to_long(df, stubnames, i, j, sep='', suffix='\d+')

Parameters:

Name Description Type Default Value Required / Optional
df The wide-format DataFrame DataFrame   Required
stubnames The stub name(s). The wide format variables are assumed to start with the stub names. str or list-like   Required
i Column(s) to use as id variable(s) str or list-like   Required
j The name of the sub-observation variable. What you wish to name your suffix in the long format. str   Required
sep A character indicating the separation of the variable names in the wide format, to be stripped from the names in the long format. For example, if your column names are A-suffix1, A-suffix2, you can strip the hyphen by specifying sep=’-‘ str Default: “” Required
suffix A regular expression capturing the wanted suffixes. ‘\d+’ captures numeric suffixes. Suffixes with no numbers could be specified with the negated character class ‘\D+’. You can also further disambiguate suffixes, for example, if your wide variables are of the form A-one, B-two,.., and you have an unrelated column A-rating, you can ignore the last one by specifying suffix=’(!? str Default: ‘\d+’ Required

Returns:DataFrame - A DataFrame that contains each stub name as a variable, with new index (i, j).

Notes: All extra variables are left untouched. This simply uses pandas.melt under the hood, but is hard-coded to “do the right thing” in a typical case.

Example:


Download the Pandas DataFrame Notebooks from here.

Previous: unique() function
Next: isna() function



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/pandas/wide_to_long.php