Pandas DataFrame DataFrame.transform() function
Python Pandas DataFrame.transform() DataFrame
applies a function on and transforms DataFrame
. The function to be applied is passed as an argument to the function. The axis length of transform()
the transformed should be the same as the original .DataFrame
DataFrame
pandas.DataFrame.transform()
Syntax
DataFrame.transform(func, axis, *args, **kwargs)
parameter
func |
It is DataFrame the function applied to . It causes DataFrame the value of to change. It can be a function, a string with a function name, a list of functions or function names, or a dictionary of axis labels. |
axis |
It is an integer or a string. It tells the rows or columns of the target axis. It can be 0 either or index for rows, 1 or columns for columns. |
*args |
These are the positional arguments to be passed to the function |
**kwargs |
These are additional keyword arguments passed to the function |
Return Value
It returns a converted DataFrame
which has the same length as the original " DataFrame
". If the returned DataFrame
has unequal lengths then the function raises ValueError
.
Sample code:DataFrame.transform()
Let's try this function first, DataFrame
adding a number to each value of .
import pandas as pd
dataframe = pd.DataFrame({
'A':
{0: 6,
1: 20,
2: 80,
3: 78,
4: 95},
'B':
{0: 60,
1: 50,
2: 7,
3: 67,
4: 54}
})
print(dataframe)
Examples DataFrame
are:
A B
0 6 60
1 20 50
2 80 7
3 78 67
4 95 54
5 98 34
This function has only one required parameter, func
. Now, we will use this function DataFrame
to add 20 to each value of .
import pandas as pd
dataframe = pd.DataFrame(
{"A": {0: 6, 1: 20, 2: 80, 3: 78, 4: 95}, "B": {0: 60, 1: 50, 2: 7, 3: 67, 4: 54}}
)
dataframe1 = dataframe.transform(func=lambda x: x + 20)
print(dataframe1)
Output:
A B
0 26 80
1 40 70
2 100 27
3 98 87
4 115 74
5 118 54
lambda
The keyword is used to declare an anonymous addition function.
Example code: DataFrame.transform()
Using sqrt
a string as a function
import pandas as pd
dataframe = pd.DataFrame(
{"A": {0: 6, 1: 20, 2: 80, 3: 78, 4: 95}, "B": {0: 60, 1: 50, 2: 7, 3: 67, 4: 54}}
)
dataframe1 = dataframe.transform(func="sqrt")
print(dataframe1)
Output:
A B
0 2.449490 7.745967
1 4.472136 7.071068
2 8.944272 2.645751
3 8.831761 8.185353
4 9.746794 7.348469
5 9.899495 5.830952
Here, lambda
instead of passing a function, we pass the function name as a string.
Example Code: DataFrame.transform()
Passing a List of Functions
import pandas as pd
dataframe = pd.DataFrame(
{"A": {0: 6, 1: 20, 2: 80, 3: 78, 4: 95}, "B": {0: 60, 1: 50, 2: 7, 3: 67, 4: 54}}
)
dataframe1 = dataframe.transform(func=["sqrt", "exp"])
print(dataframe1)
Output:
A B
sqrt exp sqrt exp
0 2.449490 4.034288e+02 7.745967 1.142007e+26
1 4.472136 4.851652e+08 7.071068 5.184706e+21
2 8.944272 5.540622e+34 2.645751 1.096633e+03
3 8.831761 7.498417e+33 8.185353 1.252363e+29
4 9.746794 1.811239e+41 7.348469 2.830753e+23
We passed a "list" of two function names ['sqrt', 'exp']
as func
. The returned DataFrame
contains two extra columns because there is an extra function.
DataFrame.apply()
vs DataFrame.transform()
Function
We can also use DataFrame.apply() function to achieve the above results. But if we compare the two functions, we can see that DataFrame.transform()
the function is more efficient when handling complex operations.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Pandas DataFrame DataFrame.query() function
Publish Date:2025/04/30 Views:107 Category:Python
-
The pandas.DataFrame.query() method filters the rows of the caller DataFrame using the given query expression. pandas.DataFrame.query() grammar DataFrame . query(expr, inplace = False , ** kwargs) parameter expr Filter rows based on query e
Pandas DataFrame DataFrame.min() function
Publish Date:2025/04/30 Views:162 Category:Python
-
Python Pandas DataFrame.min() function gets the minimum value of the DataFrame object along the specified axis. pandas.DataFrame.min() grammar DataFrame . mean(axis = None , skipna = None , level = None , numeric_only = None , ** kwargs) pa
Pandas DataFrame DataFrame.mean() function
Publish Date:2025/04/30 Views:85 Category:Python
-
Python Pandas DataFrame.mean() function calculates the mean of the values of the DataFrame object over the specified axis. pandas.DataFrame.mean() grammar DataFrame . mean(axis = None , skipna = None , level = None , numeric_only = No
Pandas DataFrame DataFrame.isin() function
Publish Date:2025/04/30 Views:133 Category:Python
-
The pandas.DataFrame.isin(values) function checks whether each element in the caller DataFrame contains values the value specified in the input . pandas.DataFrame.isin(values) grammar DataFrame . isin(values) parameter values iterable - lis
Pandas DataFrame DataFrame.groupby() function
Publish Date:2025/04/30 Views:161 Category:Python
-
pandas.DataFrame.groupby() takes a DataFrame as input and divides the DataFrame into groups based on a given criterion. We can use groupby() the method to easily process large datasets. pandas.DataFrame.groupby() grammar DataFrame . groupby
Pandas DataFrame DataFrame.fillna() function
Publish Date:2025/04/30 Views:60 Category:Python
-
The pandas.DataFrame.fillna() function replaces the values DataFrame in NaN with a certain value. pandas.DataFrame.fillna() grammar DataFrame . fillna( value = None , method = None , axis = None , inplace = False , limit = None , down
Pandas DataFrame DataFrame.dropna() function
Publish Date:2025/04/30 Views:181 Category:Python
-
The pandas.DataFrame.dropna() function removes null values (missing values) from a DataFrame by dropping rows or columns that contain null values DataFrame . NaN ( Not a Number ) and NaT ( Not a Time ) represent null values. DataFrame
Pandas DataFrame DataFrame.assign() function
Publish Date:2025/04/30 Views:55 Category:Python
-
Python Pandas DataFrame.assign() function assigns new columns to DataFrame . pandas.DataFrame.assign() grammar DataFrame . assign( ** kwargs) parameter **kwargs Keyword arguments, DataFrame the column names to be assigned to are passed as k
How to Apply a Function to a Column in a Pandas Dataframe
Publish Date:2025/04/30 Views:50 Category:Python
-
In Pandas, you can transform and manipulate columns and DataFrames using methods apply() such as transform() and . The desired transformation is passed to these methods as a function argument. Each method has its own subtle differences and