DataFrame.transform() - ( Pandas DataFrame Basics )
Heading h2
Syntax
DataFrame.transform(func, axis=0, *args, **kwargs)
Example
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
def my_func(x):
return x.mean()
df2 = df.transform(my_func)
print(df2)
Output
A B C
0 2.0 5.0 8.0
1 2.0 5.0 8.0
2 2.0 5.0 8.0
Explanation
DataFrame.transform()
is a Pandas function that applies a function to each element across one or more columns in a DataFrame and returns a transformed DataFrame with the same shape. The function can be a built-in function, a lambda function, or a user-defined function.
In the above example, we create a simple DataFrame df
with three columns and three rows. We define a function my_func
that returns the mean of a DataFrame. We then apply this function to the columns of df
using the transform()
function, which returns a new DataFrame df2
with the same shape as the original df
, but with the mean value of each column.
Use
DataFrame.transform()
is useful in Pandas for performing element-wise operations on one or more columns of a DataFrame. It is also used in data wrangling and feature engineering during data preprocessing.
Important Points
DataFrame.transform()
applies a function to each element across one or more columns in a DataFrame and returns a transformed DataFrame with the same shape- The function can be a built-in function, a lambda function, or a user-defined function
DataFrame.transform()
is useful for performing element-wise operations on one or more columns of a DataFrame, especially during data preprocessing and feature engineering
Summary
In conclusion, DataFrame.transform()
is an important function in Pandas for performing element-wise operations on one or more columns of a DataFrame. It returns a transformed DataFrame with the same shape as the original DataFrame. It is useful for performing data preprocessing and feature engineering operations such as scaling, normalization, and imputation.