pandas 1.4.2

NotesParametersRaisesReturnsBackRef
transform(self, func: 'AggFuncType', axis: 'Axis' = 0, *args, **kwargs) -> 'DataFrame | Series'

Notes

Functions that mutate the passed object can produce unexpected behavior or errors and are not supported. See gotchas.udf-mutation for more details.

Parameters

func : function, str, list-like or dict-like

Function to use for transforming the data. If a function, must either work when passed a Series or when passed to Series.apply. If func is both list-like and dict-like, dict-like behavior takes precedence.

Accepted combinations are:

  • function

  • string function name

  • list-like of functions and/or function names, e.g. [np.exp, 'sqrt']

  • dict-like of axis labels -> functions, function names or list-like 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 .

Raises

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

Returns

Series

A Series that must have the same length as self.

Call func on self producing a Series with the same axis shape as self.

See Also

Series.agg

Only perform aggregating type operations.

Series.apply

Invoke function on a Series.

Examples

This example is valid syntax, but we were not able to check execution
>>> df = pd.DataFrame({'A': range(3), 'B': range(1, 4)})
... df A B 0 0 1 1 1 2 2 2 3
This example is valid syntax, but we were not able to check execution
>>> 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:

This example is valid syntax, but we were not able to check execution
>>> s = pd.Series(range(3))
... s 0 0 1 1 2 2 dtype: int64
This example is valid syntax, but we were not able to check execution
>>> s.transform([np.sqrt, np.exp])
       sqrt        exp
0  0.000000   1.000000
1  1.000000   2.718282
2  1.414214   7.389056

You can call transform on a GroupBy object:

This example is valid syntax, but we were not able to check execution
>>> df = pd.DataFrame({
...  "Date": [
...  "2015-05-08", "2015-05-07", "2015-05-06", "2015-05-05",
...  "2015-05-08", "2015-05-07", "2015-05-06", "2015-05-05"],
...  "Data": [5, 8, 6, 1, 50, 100, 60, 120],
... })
... df Date Data 0 2015-05-08 5 1 2015-05-07 8 2 2015-05-06 6 3 2015-05-05 1 4 2015-05-08 50 5 2015-05-07 100 6 2015-05-06 60 7 2015-05-05 120
This example is valid syntax, but we were not able to check execution
>>> df.groupby('Date')['Data'].transform('sum')
0     55
1    108
2     66
3    121
4     55
5    108
6     66
7    121
Name: Data, dtype: int64
This example is valid syntax, but we were not able to check execution
>>> df = pd.DataFrame({
...  "c": [1, 1, 1, 2, 2, 2, 2],
...  "type": ["m", "n", "o", "m", "m", "n", "n"]
... })
... df c type 0 1 m 1 1 n 2 1 o 3 2 m 4 2 m 5 2 n 6 2 n
This example is valid syntax, but we were not able to check execution
>>> df['size'] = df.groupby('c')['type'].transform(len)
... df c type size 0 1 m 3 1 1 n 3 2 1 o 3 3 2 m 4 4 2 m 4 5 2 n 4 6 2 n 4
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.series.Series.apply pandas.core.series.Series.aggregate

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/series.py#4308
type: <class 'function'>
Commit: