
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Write a program in Python to transpose the index and columns in a given DataFrame
Input −
Assume you have a DataFrame, and the result for transpose of index and columns are,
Transposed DataFrame is 0 1 0 1 4 1 2 5 2 3 6
Solution 1
Define a DataFrame
Set nested list comprehension to iterate each element in the two-dimensional list data and store it in result.
result = [[data[i][j] for i in range(len(data))] for j in range(len(data[0]))
Convert the result to DataFrame,
df2 = pd.DataFrame(result)
Example
Let us see the complete implementation to get a better understanding −
import pandas as pd data = [[1,2,3],[4,5,6]] df = pd.DataFrame(data) print("Original DataFrame is\n", df) result = [[data[i][j] for i in range(len(data))] for j in range(len(data[0]))] df2 = pd.DataFrame(result) print("Transposed DataFrame is\n", df2)
Output
Original DataFrame is 0 1 2 0 1 2 3 1 4 5 6 Transposed DataFrame is 0 1 0 1 4 1 2 5 2 3 6
Solution 2
Define a DataFrame
Apply transpose method either df.T or df.transpose() to the DataFrame.
df.transpose() # or df.T
Example
Let us see the complete implementation to get a better understanding −
import pandas as pd data = [[1,2,3],[4,5,6]] df = pd.DataFrame(data) print("Original DataFrame is\n", df) print("Transposed DataFrame is\n", df.transpose())
Output
Original DataFrame is 0 1 2 0 1 2 3 1 4 5 6 Transposed DataFrame is 0 1 0 1 4 1 2 5 2 3 6
Solution 3
Define a DataFrame
Zip method returns an iterator of tuples. Apply to unzip to the two-dimensional list using * and zip it. It is defined below,
result = zip(*data)
Convert the result to DataFrame,
df2 = pd.DataFrame(result)
Example
Let us see the complete implementation to get a better understanding −
import pandas as pd data = [[1,2,3],[4,5,6]] df = pd.DataFrame(data) print("Original DataFrame is\n", df) result = zip(*data) df2 = pd.DataFrame(result) print("Transposed DataFrame is\n", df2)
Output
Original DataFrame is 0 1 2 0 1 2 3 1 4 5 6 Transposed DataFrame is 0 1 0 1 4 1 2 5 2 3 6
- Related Articles
- Write a program in Python to select any random odd index rows in a given DataFrame
- Write a program in Python to remove one or more than one columns in a given DataFrame
- Write a Python program to find the mean absolute deviation of rows and columns in a dataframe
- Write a Python program to reshape a given dataframe in different ways
- Write a program in Python to split the date column into day, month, year in multiple columns of a given dataframe
- Write a program in Python to compute grouped data covariance and calculate grouped data covariance between two columns in a given dataframe
- Write a program in Python to caluculate the adjusted and non-adjusted EWM in a given 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 shift a dataframe index by two periods in positive and negative direction
- Write a program in Python to remove first duplicate rows in a given dataframe
- Write a program in Python to modify the diagonal of a given DataFrame by 1
- Write a program in Python to count the records based on the designation in a given DataFrame
- Write a Python code to rename the given axis in a dataframe
- Write a program in Python to perform flatten the records in a given dataframe by C and F order

Advertisements