- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a Python program to reshape a given dataframe in different ways
We can reshape a dataframe using melt(),stack(),unstack() and pivot() function.
Solution 1
Define a dataframe.
Apply melt() function to convert wide dataframe column as rows. It is defined below,
df.melt()
Example
Let’s see the below code to get a better understanding −
import pandas as pd df = pd.DataFrame({'Id':[1,2,3],'Age':[13,14,13],'Mark':[80,90,85]}) print("Dataframe is:\n",df) print(df.melt())
Output
Dataframe is: Id Age Mark 0 1 13 80 1 2 14 90 2 3 13 85 variable value 0 Id 1 1 Id 2 2 Id 3 3 Age 13 4 Age 14 5 Age 13 6 Mark 80 7 Mark 90 8 Mark 85
Solution 2
Define a dataframe.
Apply stack() function to increase the level of the index in a dataframe. It is defined below,
df.stack().to_frame()
If you want to revert back the changes, you can use unstack().
df.unstack().to_frame()
Example
Let’s see the below implementation to get a better understanding −
import pandas as pd df = pd.DataFrame({'Id':[1,2,3],'Age':[13,14,13],'Mark':[80,90,85]}) print("Dataframe is:\n",df) print(df.stack().to_frame()) print(df.unstack().to_frame())
Output
Dataframe is: Id Age Mark 0 1 13 80 1 2 14 90 2 3 13 85 0 0 Id 1 Age 13 Mark 80 1 Id 2 Age 14 Mark 90 2 Id 3 Age 13 Mark 85 0 Id 0 1 1 2 2 3 Age 0 13 1 14 2 13 Mark 0 80 1 90 2 85
Solution 3
Define a dataframe
Apply pivot() function to reshape a dataframe based on Id column,
df.pivot(columns='Id')
Example
Let’s see the below implementation to get a better understanding −
import pandas as pd df = pd.DataFrame({'Id':[1,2,3],'Age':[13,14,13],'Mark':[80,90,85]}) print("Dataframe is:\n",df) print(df.pivot(columns='Id'))
Output
Dataframe is: Id Age Mark 0 1 13 80 1 2 14 90 2 3 13 85 Age Mark Id 1 2 3 1 2 3 0 13.0 NaN NaN 80.0 NaN NaN 1 NaN 14.0 NaN NaN 90.0 NaN 2 NaN NaN 13.0 NaN NaN 85.0
- Related Articles
- Python – Reshape the data in a Pandas DataFrame
- Write a program in Python to convert a given dataframe to a LaTex document
- Write a program in Python to localize Asian timezone for a given dataframe
- Write a program in Python to remove first duplicate rows in a given dataframe
- Write a program in Python to transpose the index and columns in a given DataFrame
- Write a Python program to sort a given DataFrame by name column in descending order
- Write a program in Python to modify the diagonal of a given DataFrame by 1
- Write a program in Python to select any random odd index rows in a given DataFrame
- Write a Python code to rename the given axis in a dataframe
- Write a Python code to filter palindrome names in a given dataframe
- Write a program in Python to count the total number of leap years in a given DataFrame
- Write a program in Python to count the records based on the designation in a given DataFrame
- Write a program in Python to remove one or more than one columns in a given DataFrame
- Write a program in Python to caluculate the adjusted and non-adjusted EWM in a given dataframe
- Write a Python code to swap last two rows in a given dataframe

Advertisements