Saturday, November 16, 2024
Google search engine
HomeLanguagesReshape a pandas DataFrame using stack,unstack and melt method

Reshape a pandas DataFrame using stack,unstack and melt method

Pandas use various methods to reshape the dataframe and series. 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 module
import pandas as pd
 
# making dataframe
 
# it was print the first 5-rows
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

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 module
import pandas as pd
 
# making dataframe
df = pd.read_csv("nba.csv")
 
# reshape the dataframe using stack() method
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

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 module
import pandas as pd
 
# making dataframe
df = pd.read_csv("nba.csv")
 
# unstack() method
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

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 module
import pandas as pd
 
# making dataframe
df = pd.read_csv("nba.csv")
 
# it takes two columns "Name" and "Team"
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

RELATED ARTICLES

Most Popular

Recent Comments