Updated April 3, 2023
Introduction to Pandas DataFrame.transpose()
When every value of the pandas data structure needs to be transposed in some specific manner then the pandas.transpose() function can be used. The word transpose means the process of exchanging places. same as the word means the transpose() method in pandas is used for interchanging the axes. the process of interchanging the axes means all column values in the dataframe will get depicted as rows and all rows in the dataframe will be reflected as columns. this interchanging process is named as transpose.
Syntax and Parameter
Syntax and parameter of pandas dataframe.transpose() are:
Syntax:
DataFrame.transpose(self, *args, copy: bool = False)
Parameter:
- args: In some instances there exist possibilities where the compatibility needs to be maintained between the numpy and the pandas dataframe and this argument is implied at those points of time more specifically to mention.
- Copy: When a data frame has only a single specific datatype in it, this option allows to determine whether the transposing function needs to be carried on even on the case of this single data type. the default value of this option is false and setting this to true for mixed data types will allow the transpose to happen. for a dataframe with mixed data type items the value of this option or this argument is considered as default.
- returns: The final transposed dataframe is what has been represented here. it mentions the return of the final transposed dataframe.
Examples of Pandas DataFrame.transpose()
Following are the examples are given below:
Example #1
Code:
import pandas as pd
Core_Series = pd.Series([ 10, 20, 30, 40, 50, 60])
print(" THE CORE SERIES ")
print(Core_Series)
Transposed_Series = Core_Series.transpose()
print("")
print(" THE TRANSPOSED SERIES ")
print(Transposed_Series)
Output:
Code Explanation: Here the pandas library is initially imported and the imported library is used for creating a series. The values in the series are formulated in such a way that they are a series of 10 to n. the series involves an increase in the values by 10. The transpose method is applied over the core series. basically, since a series data structure is not of a row, column format the impact of the transpose() function on the core series is not to any extent. this means that the core dataframe does not face any change in its axis or in its row-column structure as a result of the transpose function. we can also notice in the given output snap that the series data structure did not face any structural change and it the original series itself gets printed on to the console.
Example #2
Code:
import pandas as pd
Core_Dataframe = pd.DataFrame({'A' : [ 1, 6, 11, 15, 21, 26],
'B' : [2, 7, 12, 17, 22, 27],
'C' : [3, 8, 13, 18, 23, 28],
'D' : [4, 9, 14, 19, 24, 29],
'E' : [5, 10, 15, 20, 25, 30]})
print(" THE CORE DATAFRAME ")
print(Core_Dataframe)
Transposed_Dataframe = Core_Dataframe.transpose()
print("")
print(" THE TRANSPOSED DATAFRAME ")
print(Transposed_Dataframe)
Output:
Code Explanation: Here again the values in the dataframe are formulated in such a way that they are a series of 1 to n. Here the transpose function is applied over the core dataframe and we can notice how the core dataframes rows and columns get finitely interchanged in this process. So this means all the rows in the dataframe become as columns and all columns in the dataframe are positioned as rows at the end of the dataframe transpose process. we can notice this in the disposition of the column values in the output console.
Example #3
Code:
import pandas as pd
Core_Dataframe = pd.DataFrame({'A' : [10, 'A', 60.5, 'F', 110, 'K'],
'B' : [20, 'B', 70.7, 'G', 120, 'L'],
'C' : [30, 'C', 80.5, 'H', 130, 'M'],
'D' : [40, 'D', 90.5, 'I', 140, 'N'],
'E' : [50, 'E', 100.5, 'J', 150, 'O']})
print(" THE CORE DATAFRAME ")
print(Core_Dataframe)
print(Core_Dataframe.dtypes)
Transposed_Dataframe_without_copy_parm = Core_Dataframe.transpose(copy='True')
print("")
print(" THE TRANSPOSED DATAFRAME WITHOUT COPY PARM")
print(Transposed_Dataframe_without_copy_parm)
print(Transposed_Dataframe_without_copy_parm.dtypes)
Output:
Code Explanation: Here in this example the generated dataframe is associated with mixed values or datatypes , this means it involves values of float, string, and integer within it. A transpose process is applied over this dataframe with the copy option set to true which means the original value will be allowed to be copied here also, the data type of the dataframe columns is also printed on to the console before and after the transpose process. from the printed output we can notice the dataframe values are flexibly transposed when printed on to the console. this can be determined by all row values depicted as column values and all column values depicted as row values in the final transposed dataframe.
Example #4
Code:
import pandas as pd
Core_Dataframe = pd.DataFrame({'Emp_No' : ['Emp1','Emp2','Emp3','Emp4'],
'Employee_Name' : ['Arun', 'selva', 'rakesh', 'arjith'],
'Employee_dept' : ['CAD', 'CAD', 'DEV', 'CAD']})
Core_Dataframe.set_index('Emp_No',inplace=True)
print(" THE CORE DATAFRAME ")
print(Core_Dataframe)
print("")
print(Core_Dataframe.dtypes)
Transposed_Dataframe = Core_Dataframe.transpose(copy='True')
print("")
print(" THE TRANSPOSED DATAFRAME ")
print(Transposed_Dataframe)
print("")
print(Transposed_Dataframe.dtypes)
Output:
Code Explanation: In this example, a much better-organized dataframe is considered as input. The data here used here is more meaningful and it is employee data. the data comprises three columns namely the employee number, employee name, and the employee department. here the employee number column is set as the index of the dataset. So the initial index values of 1 to n gets replaced with the values of employee numbers, we can notice this in the console on the printing of the core dataframe used. So in the core dataframe all the index values are nicely replaced by means of their corresponding employee numbers and whereas the employee name and department remain as the remaining two columns in the dataframe. the transpose process on the dataframe makes all the employee numbers as the column values and the previously declared column headings become the column rows. this transposed output can be noticed in the console.
Recommended Articles
We hope that this EDUCBA information on “Pandas DataFrame.transpose()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.