DataFrame.
transform
Call func on self producing a DataFrame with transformed values.
func
Produced DataFrame will have same axis length as self.
Function to use for transforming the data. If a function, must either work when passed a DataFrame or when passed to DataFrame.apply.
Accepted combinations are:
function
string function name
list of functions and/or function names, e.g. [np.exp. 'sqrt']
[np.exp. 'sqrt']
dict of axis labels -> functions, function names or list of such.
If 0 or ‘index’: apply function to each column. If 1 or ‘columns’: apply function to each row.
Positional arguments to pass to func.
Keyword arguments to pass to func.
A DataFrame that must have the same length as self.
See also
DataFrame.agg
Only perform aggregating type operations.
DataFrame.apply
Invoke function on a DataFrame.
Examples
>>> df = pd.DataFrame({'A': range(3), 'B': range(1, 4)}) >>> df A B 0 0 1 1 1 2 2 2 3 >>> df.transform(lambda x: x + 1) A B 0 1 2 1 2 3 2 3 4
Even though the resulting DataFrame must have the same length as the input DataFrame, it is possible to provide several input functions:
>>> s = pd.Series(range(3)) >>> s 0 0 1 1 2 2 dtype: int64 >>> s.transform([np.sqrt, np.exp]) sqrt exp 0 0.000000 1.000000 1 1.000000 2.718282 2 1.414214 7.389056