You are on page 1of 18

RBVRR Women's

College

 M. Lucy Pravallika Reddy


 11720506009
 MSc–DataScience (III
Sem )
Unstacking Groups In Data
Visualization
Stacked Data:
Narrow, or stacked data.
Example:
Unstacked Data:
 Unstacked data is presented with each different data variable in a separate column. Narrow, or
stacked data is presented with one column containing all the values and another column listing
the context of the value.

Example:
 To Unstack the data we use pandas GroupBy -Unstack() function.

 Pandas Unstack is a function that pivots the level of the indexed columns in
a stacked data frame. A stacked data frame is usually a result of an
aggregated groupby function in pandas. Stack() sets the columns to a new
level of hierarchy whereas Unstack() pivots the indexed column.
 Thereare different ways to Unstack a pandas dataframe which
would be discussed in the below methods.
 Method 1: General Unstacking of pandas dataframe at multi-
levels using unstack().
Groupby aggregation on a dataframe usually returns a stacked
dataframe object, of multi-levels depending on the aggregation
model.
Output:
Code Explanation:
•Create a sample dataframe showing car sales in two quarters.
•Now, stack the dataframe using stack() function, this will stack the columns to
row values.
•As we have two columns while unstacking it will be considered as two different
levels.
•Now, use unstack function with level 0 and level 1 separately to stack the
dataframe at two different levels.
•It depends on the use case to stack the first or second level.
Method 2: GroupBy Unstacking of pandas dataframe with
simple unstack().
Whenever we use groupby function on pandas dataframe with
more than one aggregation function per column, the output is
usually a multi-indexed column where as the first index
specifies the column name and the second column index
specifies the aggregation function name.
Output:
Code Explanation:
•Create a sample dataframe showing car sales in two quarters.
•Use GroupBy function to group the car sales data by sum min and max sales of
two quarters as shown
•As we have two columns while unstacking it will be considered as two different
levels at two indexes. The first index will have the column name and the second
index will have the name of the aggregated function.
•Now, perform a simple unstack operation on the grouped dataframe. This simple
unstack will convert the columns as rows and vice versa as shown in the output
Method 3: GroupBy Unstacking of pandas dataframe
with multiple unstack() at two different levels.
Generally, to have more depth in insights generated by
GroupBy function, it is normally stacked at different levels
of the grouped dataframe. This grouped dataframe can be
further investigated by unstacking at different levels using
unstack() function.
Output:
Code Explanation:
•Create a sample dataframe showing car sales in two quarters.
•Use GroupBy function to group the car sales data by sum min and max sales of
two quarters as shown
•As we have two columns while unstacking it will be considered as two different
levels at two indexes. The first index will have the column name and the second
index will have the name of the aggregated function.
•Now, use stack() at level 0 of the grouped dataframe and unstack() the grouped
dataframe.
•Then, use stack() at level 1 of the grouped dataframe and unstack() the grouped
dataframe.
•It depends on the use case to unstack the dataframe at the first or second level.
Thank
You !!

You might also like