Reshape a Pandas DataFrame using stack,unstack and melt method
Last Updated :
14 Dec, 2023
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 into columns. On the other hand, the melt method is used to transform wide-format data into a long-format, making it suitable for various analytical tasks. Let’s see about some of that reshaping method.
Importing the Dataset
To download the dataset used in this article click here .
Python3
import pandas as pd
print(df.head())
Output:
Name Team Number Position Age Height Weight College Salary 0 Avery Bradley Boston Celtics 0.0 PG 25.0 6-2 180.0 Texas 7730337.0 1 Jae Crowder Boston Celtics 99.0 SF 25.0 6-6 235.0 Marquette 6796117.0 2 John Holland Boston Celtics 30.0 SG 27.0 6-5 205.0 Boston University NaN 3 R.J. Hunter Boston Celtics 28.0 SG 22.0 6-5 185.0 Georgia State 1148640.0 4 Jonas Jerebko Boston Celtics 8.0 PF 29.0 6-10 231.0 NaN 5000000.0
Reshape DataFrame in Pandas
Below are the three methods that we will use to reshape the layout of tables in Pandas :
Using Pandas stack() method
Using unstack() method
Using melt() method
Reshape the Layout of Tables in Pandas Using stack() method
The stack() method works with the MultiIndex objects in DataFrame, it returns a DataFrame with an index with a new inner-most level of row labels. It changes the wide table to a long table.
Python3
import pandas as pd
df = pd.read_csv("nba.csv")
df_stacked = df.stack()
print(df_stacked.head(26))
Output:
0 Name Avery Bradley Team Boston Celtics Number 0.0 Position PG Age 25.0 Height 6-2 Weight 180.0 College Texas Salary 7730337.0 1 Name Jae Crowder Team Boston Celtics Number 99.0 Position SF Age 25.0 Height 6-6 Weight 235.0 College Marquette Salary 6796117.0 2 Name John Holland Team Boston Celtics Number 30.0 Position SG Age 27.0 Height 6-5 Weight 205.0 College Boston University dtype: object
Reshape a Pandas DataFrame Using unstack() method
The unstack() is similar to stack method, It also works with multi-index objects in dataframe, producing a reshaped DataFrame with a new inner-most level of column labels.
Python3
import pandas as pd
df = pd.read_csv("nba.csv")
df_unstacked = df_stacked.unstack()
print(df_unstacked.head(10))
Output :
Name Team Number Position Age Height Weight College Salary 0 Avery Bradley Boston Celtics 0.0 PG 25.0 6-2 180.0 Texas 7730337.0 1 Jae Crowder Boston Celtics 99.0 SF 25.0 6-6 235.0 Marquette 6796117.0 2 John Holland Boston Celtics 30.0 SG 27.0 6-5 205.0 Boston University NaN 3 R.J. Hunter Boston Celtics 28.0 SG 22.0 6-5 185.0 Georgia State 1148640.0 4 Jonas Jerebko Boston Celtics 8.0 PF 29.0 6-10 231.0 NaN 5000000.0 5 Amir Johnson Boston Celtics 90.0 PF 29.0 6-9 240.0 NaN 12000000.0 6 Jordan Mickey Boston Celtics 55.0 PF 21.0 6-8 235.0 LSU 1170960.0 7 Kelly Olynyk Boston Celtics 41.0 C 25.0 7-0 238.0 Gonzaga 2165160.0 8 Terry Rozier Boston Celtics 12.0 PG 22.0 6-2 190.0 Louisville 1824360.0 9 Marcus Smart Boston Celtics 36.0 PG 22.0 6-4 220.0 Oklahoma State 3431040.0
Reshape the Layout of Tables in Pandas Using melt() method
The melt() in Pandas reshape dataframe from wide format to long format. It uses the “id_vars[‘col_names’]” to melt the dataframe by column names.
Python3
import pandas as pd
df = pd.read_csv("nba.csv")
df_melt = df.melt(id_vars=['Name', 'Team'])
print(df_melt.head(10))
Output:
Name Team variable value 0 Avery Bradley Boston Celtics Number 0.0 1 Jae Crowder Boston Celtics Number 99.0 2 John Holland Boston Celtics Number 30.0 3 R.J. Hunter Boston Celtics Number 28.0 4 Jonas Jerebko Boston Celtics Number 8.0 5 Amir Johnson Boston Celtics Number 90.0 6 Jordan Mickey Boston Celtics Number 55.0 7 Kelly Olynyk Boston Celtics Number 41.0 8 Terry Rozier Boston Celtics Number 12.0 9 Marcus Smart Boston Celtics Number 36.0
Like Article
Suggest improvement
Share your thoughts in the comments
Add Your Comment
Please Login to comment...