The Wayback Machine - https://web.archive.org/web/20240717223420/https://www.geeksforgeeks.org/how-to-convert-wide-dataframe-to-tidy-dataframe-with-pandas-stack/
Open In App

How to Convert Wide Dataframe to Tidy Dataframe with Pandas stack()?

Last Updated : 26 Nov, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

We might sometimes need a tidy/long-form of data for data analysis. So, in python’s library Pandas there are a few ways to reshape a dataframe which is in wide form into a dataframe in long/tidy form. Here, we will discuss converting data from a wide form into a long-form using the pandas function stack(). stack() mainly stacks the specified index from column to index form. And it returns a reshaped DataFrame or even a series having a multi-level index with one or more new inner-most levels compared to the current DataFrame, these levels are created by pivoting the columns of the current dataframe and outputs a:

  • Series: if the columns have a single level
  • DataFrame: if the columns have multiple levels, then the new index level(s) is (are) taken from the specified level(s).

Syntax: DataFrame.stack(level=- 1, dropna=True)

Parameters –

  • level : It levels to stack from the column axis to the index axis. It either takes an int, string or list as input value. And by default is set to -1.
  • dropna : It asks whether to drop the rows into the resulting dataFrame or series in case they don’t have any values. It is of bool type and by default set to True.

Returns a stacked DataFrame or series.

Now, let’s start coding!

Case 1#:

Firstly, let’s start with a simple single-level column and a wide form of data.

Python3




import pandas as pd
  
# Single level columns
df_single_level_cols = pd.DataFrame([[74, 80], [72, 85]],
                                    index=['Deepa', 'Balram'],
                                    columns=['Maths', 'Computer'])
print(df_single_level_cols)


Output

Now, after we apply the stack() function Then we will get a dataframe with a single level column axis returns a Series:

Python3




# Single level with stack()
df_single_level_cols.stack()


Output:

Case 2#:

Let’s now try out with multi-level columns.

Python3




# Simple Multi-level columns
multicol1 = pd.MultiIndex.from_tuples([('Science', 'Physics'),
                                       ('Science', 'Chemistry')])
  
df_multi_level_cols1 = pd.DataFrame([[80, 64], [76, 70]],
                                    index=['Deepa', 'Balram'],
                                    columns=multicol1)
  
print(df_multi_level_cols1)


Output:

After stacking the dataframe with a multi-level column axis:

Python3




# Multi-level stacking with stack
df_multi_level_cols1.stack()


Output:

Case 3#:

Now, let’s try with some missing values In the regular wide form, we will get the values as it is, since it has lesser value than the stacked forms:

Python3




# Multi-level with missing values
multicol2 = pd.MultiIndex.from_tuples([('English', 'Literature'),
                                       ('Hindi', 'Language')])
  
df_multi_level_cols2 = pd.DataFrame([[80, 75], [80, 85]],
                                    index=['Deepa', 'Balram'],
                                    columns=multicol2)
df_multi_level_cols2


Output:

But when we stack it,

We can have missing values when stacking a dataframe with multi-level columns, as the stacked dataframe typically has more values than the original dataframe. Missing values are filled with NaNs, like here in this example, the value for the English Language is not known so is filled with NaN.

Python3




# Multi-level missing values as NaN
df_multi_level_cols2.stack()


Output:

Case 4#:

Apart from that, we can also contain the stacked values as per our preferences, hence by prescribing the stack the value to be kept. The first parameter actually controls which level or levels are stacked. Like,

Python3




# Prescribing the level(s) to be stacked
df_multi_level_cols2.stack(0)
  
# The first parameter controls which level 
# or levels are stacked
df_multi_level_cols2.stack([0, 1])


Output:

Case 5#:

Now, finally let’s check what is the purpose of the dropna in stack(). For this, we will drop the rows which have completely NaN values. Let’s check the code for the regular result when the NaN values are included.

Python3




# Dropping missing values
df_multi_level_cols3 = pd.DataFrame([[None, 80], [77, 82]],
                                    index=['Deepa', 'Balram'],
                                    columns=multicol2)
print(df_multi_level_cols3)
  
# contains the row with all NaN values since,
# dropna=False
df_multi_level_cols3.stack(dropna=False)
print(df_multi_level_cols3)


Output:

Here, we can see in the Deepa index, the value for Literature is NaN when we are operating with dropna = False (it is including the NaN value as well)

Let’s check when we make the dropna = True( omits the complete NaN values row)

Python3




# Drops the row with completely NaN values
df_multi_level_cols3.stack(dropna=True)
print(df_multi_level_cols3)


Output:

So, here making dropna = False omits the Literature row as a whole since it was NaN completely.



Similar Reads

Reshape Wide DataFrame to Tidy with identifiers using Pandas Melt
Sometimes we need to reshape the Pandas data frame to perform analysis in a better way. Reshaping plays a crucial role in data analysis. Pandas provide functions like melt and unmelt for reshaping. In this article, we will see what is Pandas Melt and how to use it to reshape wide to Tidy with identifiers. Pandas Melt(): Pandas.melt() unpivots a Dat
3 min read
Pandas Dataframe.to_numpy() - Convert dataframe to Numpy array
Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). This data structure can be converted to NumPy ndarray with the help of the DataFrame.to_numpy() method. In this article we will see how to convert dataframe to numpy array. Syntax of Pandas DataFrame.to_numpy()
3 min read
Convert given Pandas series into a dataframe with its index as another column on the dataframe
First of all, let we understand that what are pandas series. Pandas Series are the type of array data structure. It is one dimensional data structure. It is capable of holding data of any type such as string, integer, float etc. A Series can be created using Series constructor. Syntax: pandas.Series(data, index, dtype, copy) Return: Series object.
1 min read
Pandas DataFrame to_dict() Method | Convert DataFrame to Dictionary
Python is a great language for doing data analysis because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.  Pandas .to_dict() method is used to convert a DataFrame into a dictionary of series or list-like data type depending on the orient parameter. Exam
3 min read
Reshape a Pandas DataFrame using stack,unstack and melt method
Pandas use various methods to reshape the dataframe and series. Reshaping a Pandas DataFrame is a common operation to transform data structures for better analysis and visualization. The stack method pivots columns into rows, creating a multi-level index Series. Conversely, the unstack method reverses this process by pivoting inner index levels int
5 min read
Pandas DataFrame hist() Method | Create Histogram in Pandas
A histogram is a graphical representation of the numerical data. Sometimes you'll want to share data insights with someone, and using graphical representations has become the industry standard. Pandas.DataFrame.hist() function plots the histogram of a given Data frame. It is useful in understanding the distribution of numeric variables. This functi
4 min read
Pandas DataFrame iterrows() Method | Pandas Method
Pandas DataFrame iterrows() iterates over a Pandas DataFrame rows in the form of (index, series) pair. This function iterates over the data frame column, it will return a tuple with the column name and content in the form of a series. Example: Python Code import pandas as pd df = pd.DataFrame({ 'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 32, 3
2 min read
Pandas DataFrame interpolate() Method | Pandas Method
Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.  Python Pandas interpolate() method is used to fill NaN values in the DataFrame or Series using various interpolation techniques to fill the m
3 min read
Pandas DataFrame duplicated() Method | Pandas Method
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas duplicated() method identifies duplicated rows in a DataFrame. It returns a boolean series which is True only for unique rows. Ex
3 min read
Difference Between Spark DataFrame and Pandas DataFrame
Dataframe represents a table of data with rows and columns, Dataframe concepts never change in any Programming language, however, Spark Dataframe and Pandas Dataframe are quite different. In this article, we are going to see the difference between Spark dataframe and Pandas Dataframe. Pandas DataFrame Pandas is an open-source Python library based o
3 min read
Practice Tags :
three90RightbarBannerImg