Pandas DataFrame.transform

We can define Pandas DataFrame as a two-dimensional size-mutable, heterogeneous tabular data structure with some labeled axes (rows and columns). Performing the arithmetic operations will align both row and column labels. It can be considered as a dict-like container for Series objects.

The main task of Pandas DataFrame.transform() function is to self produce a DataFrame with its transformed values and it has the same axis length as self.

Syntax:

Parameters :

func : It is a function that is used for transforming the data.

axis : Refers to 0 or 'index', 1 or 'columns', default value 0.

*args: It is a positional arguments that is to be passed to a func.

**kwargs : It is a keyword arguments that is to be passed to a func.

Returns:

It returns the DataFrame that must have same length as self.

Example 1 : Use DataFrame.transform() function to add 10 to each element in the dataframe.

Output:

       P       Q        R       S
A_Row 8.0      4.0      2.0    16.0
B_Row  2.0     14.0    5.0     10.0
C_Row  9.0     12.0    7.0     NaN
D_RowNaN   22.0    16.0   19.0
E_Row  3.0NaN    13.0   18.0

Example 2 : Use DataFrame.transform() function to find the square root and the result of euler's number raised to each element of the dataframe.

Output:

        P       Q       R       S
A_Row  88.0     14.0    12.0    16.0
B_Row  12.0     14.0    15.0     10.0
C_Row  19.0     22.0    17.0     NaN
D_RowNaN     21.0    16.0    19.0
E_Row  13.0NaN    13.0   18.0





Latest Courses