Pandas Series transform() Function:
The transform() function of Pandas Series invokes func on self to generate a Series with transformed data.
Syntax:
Series.transform(func, axis=0)
Parameters
func: This is required. It Indicates the function that will be used to transform the data. If a function, must either work when passed a Series or when passed to Series.apply. When func has both list-like and dict-like behavior, the dict-like behavior takes precedence. The following are acceptable combinations.
- function
- string function name
- list of functions and/or function names, e.g. [np.exp, ‘sqrt’]
- dictionary of axis labels -> functions, function names, or list of such.
axis: This is optional. It indicates a value of 0 or ‘index’. This is an axis on which the function is applied.
Return Value:
A Series with transformed values of the same length as self is returned by the transform() function of Pandas Series.
Note: If the returned Series is not the same length as self, ValueError is returned.
- Python Pandas Series ge() Function
- Python Pandas DataFrame iteritems() Function
- Python Pandas DataFrame mul() Function
Pandas Series transform() Function in Python
Example1
Approach:
- Import pandas module using the import keyword.
- Pass some random list as an argument to the Series() function of the pandas module to create a series.
- Store it in a variable.
- Print the above given series
- Pass some random lambda function to the transform() function, apply it on the given series to get the Series with transformed data and print the result.
- Here the lambda function adds 5 to each element of the series.
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword. import pandas as pd # Pass some random list as an argument to the Series() function # of the pandas module to create a series. # Store it in a variable. gvn_series = pd.Series([10, 5, 2, 4]) # Print the above given series print("The given series is:") print(gvn_series) print() # Pass some random lambda function to the transform() function, # apply it on the given series to get the Series with transformed data # and print the result. # Here the lambda function adds 5 to each element of the series. print("The given Series with transformed data:") print(gvn_series.transform(lambda gvn_series: gvn_series+5))
Output:
The given series is: 0 10 1 5 2 2 3 4 dtype: int64 The given Series with transformed data: 0 15 1 10 2 7 3 9 dtype: int64
Example2
A Series can be subjected to multiple operations at the same time. Here, two operations: ‘sqrt’ (square root) and ‘cbrt’ (cube root) are applied at the same time, and each produces a Series of the same length.
Approach:
- Import pandas module using the import keyword.
- Pass some random list as an argument to the Series() function of the pandas module to create a series.
- Store it in a variable.
- Print the above-given series.
- Pass some random list of operations to the transform() function, apply it on the given series to get the Series with transformed data, and print the result.
- Here the squareroot, cuberoot operations are performed on each element of the series.
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword. import pandas as pd # Pass some random list as an argument to the Series() function # of the pandas module to create a series. # Store it in a variable. gvn_series = pd.Series([10, 125, 16, 4]) # Print the above given series print("The given series is:") print(gvn_series) print() # Pass some random list of operations to the transform() function, # apply it on the given series to get the Series with transformed data # and print the result. # Here the squareroot, cuberoot operations are performed on each element of the series. print("The squareroot, cuberoot values of each element of the series:") print(gvn_series.transform(['sqrt', 'cbrt']))
Output:
The given series is: 0 10 1 125 2 16 3 4 dtype: int64 The squareroot, cuberoot values of each element of the series: sqrt cbrt 0 3.162278 2.154435 1 11.180340 5.000000 2 4.000000 2.519842 3 2.000000 1.587401
Example3
Approach:
- Import pandas module using the import keyword.
- Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
- Store it in a variable.
- Print the given dataframe.
- Apply transform() function on the student_marks column of the dataframe by passing ‘sqrt’ as an argument to it to get the squareroot of all the values of the student_marks column and print the result.
-
The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword. import pandas as pd # Pass some random key-value pair(dictionary), index list as arguments to the # DataFrame() function of the pandas module to create a dataframe # Store it in a variable. data_frme = pd.DataFrame({ "student_rollno": [1, 2, 3, 4], "student_marks": [64, 45, 25, 90]}, index= ["virat", "nick" , "jessy", "sindhu"] ) # Print the given dataframe print("The given Dataframe:") print(data_frme) print() # Apply transform() function on the student_marks column of the dataframe # by passing 'sqrt' as an argument to it to get the squareroot of all the values # of the student_marks column and print the result. print("The squareroot of all the values of the student_marks column of the dataframe:") print(data_frme['student_marks'].transform('sqrt'))
Output:
The given Dataframe: student_rollno student_marks virat 1 64 nick 2 45 jessy 3 25 sindhu 4 90 The squareroot of all the values of the student_marks column of the dataframe: virat 8.000000 nick 6.708204 jessy 5.000000 sindhu 9.486833 Name: student_marks, dtype: float64