W3cubDocs

/pandas 0.25

pandas.Series.transform

Series.transform(self, func, axis=0, *args, **kwargs) [source]

Call func on self producing a Series with transformed values and that has the same axis length as self.

New in version 0.20.0.

Parameters:
func : function, str, list or dict

Function to use for transforming the data. If a function, must either work when passed a Series or when passed to Series.apply.

Accepted combinations are:

  • function
  • string function name
  • list of functions and/or function names, e.g. [np.exp. 'sqrt']
  • dict of axis labels -> functions, function names or list of such.
axis : {0 or ‘index’}

Parameter needed for compatibility with DataFrame.

*args

Positional arguments to pass to func.

**kwargs

Keyword arguments to pass to func.

Returns:
Series

A Series that must have the same length as self.

Raises:
ValueError : If the returned Series has a different length than self.

See also

Series.agg
Only perform aggregating type operations.
Series.apply
Invoke function on a Series.

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 Series must have the same length as the input Series, 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

© 2008–2012, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
Licensed under the 3-clause BSD License.
https://pandas.pydata.org/pandas-docs/version/0.25.0/reference/api/pandas.Series.transform.html