Pandas DataFrame | transform method
Start your free 7-days trial now!
Pandas DataFrame.transform(~) method applies a function to transform the rows or columns of the source DataFrame. Note that a new DataFrame is returned, and the source DataFrame is kept intact.
Parameters
1. func | function or string or list or dict
The transformation applied to the rows or columns of the source DataFrame. If a function is passed, then it takes in as argument a Series or a DataFrame.
The allowed values are as follows:
a function (e.g.
np.mean)the name of the function as a string (e.g.
"np.mean")a list of the above two (e.g.
[np.mean, "np.max"])dictionary with:
key: row/column label
value: function, function name or list of such
2. axis | list | optional
Whether to apply the transformation row-wise or column-wise:
Axis | Description |
|---|---|
| Transform each column. |
| Transform each row. |
By default, axis=0.
3. args | any
The positional arguments you want to pass to func.
4. kwargs | any
The keyword arguments you want to pass to func.
Return Value
A new DataFrame that has the same shape as the source DataFrame.
Examples
Basic usage
Consider the following DataFrame:
df
A B0 -3 51 4 -6
Applying NumPy's abs(~) method, which returns the absolute value of the input:
A B0 3 51 4 6
Passing in a function
Consider the following DataFrame:
df
A B0 3 51 4 6
We can pass in a custom function like so:
A B0 3 101 4 12
Here, our custom function foo takes in as argument col, which is a single column of the DataFrame (Series).
Passing in multiple functions
Consider the following DataFrame:
df
A B0 -3 -51 4 6
To apply multiple transformations, pass in a list like so:
A B absolute <lambda_0> absolute <lambda_0>0 3 -2 5 -41 4 5 6 7
Notice how the two transformations are independent, that is, both transformations are applied to the original values.
Transforming each column
Consider the following DataFrame:
df
A B0 3 51 4 6
By default, axis=0, which means that we are transforming each column:
A B0 3 101 4 12
Transforming each row
Consider the same DataFrame as before:
df
A B0 3 51 4 6
To transform each row, pass in axis=1 like so:
def foo(row): # row is a Series representing a single row
df.transform(foo, axis=1)
A B0 3 51 8 12
Transforming certain columns only
Consider the same DataFrame as before:
df
A B C0 3 5 71 4 6 8
By default, the transform(~) method will either transform all columns (axis=0) or all rows (axis=1).
To transform certain columns, select the columns you wish to transform first:
def foo(val): return val * 3
# Here, we are transforming just columns A and Bdf_new_cols = df[["A","B"]].transform(foo)df_new_cols
A B0 9 151 12 18
Here, a new DataFrame with the transformed columns is returned while the original DataFrame df is left intact. If you wanted to replace the original columns, then:
def foo(val): return val * 3
df_new_cols = df[["A","B"]].transform(foo)df[["A","B"]] = df_new_colsdf
A B C0 9 15 71 12 18 8
Passing in arguments
Consider the following DataFrame:
df
A B0 3 51 4 6
To pass in arguments to func:
A B0 3 101 4 12