Reshaping DataFrames With Pandas
Introduction
In order to conduct different deep-dive analyses, transforming data table is an unavoidable step. In this article, I will show you some Pandas DataFrame Basic of Reshaping Data. I hope this would be some of help for your analytical tasks in the future.
Reshaping with Hierarchical Indexing
Stack() and Unstack() are 2 ways to rearrange data in a data frame with multi-index rows and columns.
- Stack(): Rotating the innermost column index to become the innermost row index.
- Unstack(): The inverse action of Stack(). This function pivots the rows into the columns.
Single Column Level
I will depict how these 2 functions generate in the example below. But first, let’s create a simple data frame.
df = pd.DataFrame([[7, 9], [11, 13]], index=['Alex', 'Jack'], columns=['Books', 'Pens'])
By applying the stack method on this data, the columns are changed into the rows, creating a series.
From df1, we can get back to the original data with unstack().
Multi-level Column Level
Now, let’s create a new data frame.
#Create multi-level…