Pandas Tutorial Pandas References

Pandas Series - transform() function



The Pandas Series transform() function calls func on self and produce a Series with transformed values.

Syntax

Series.transform(func, axis=0)

Parameters

func Required. Specify function used 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 of functions and/or function names, e.g. [np.exp, 'sqrt']
  • dictionary of axis labels -> functions, function names or list of such.
axis Optional. Specify {0 or 'index'}. Specify axis for the function to be applied on.

Return Value

Returns a Series of same length as self with transformed values.

Exceptions

Returns ValueError, if the returned Series has a different length than self.

Example: using transform() on a Series

In the example below, the transform() function is applied on a Series.

import pandas as pd
import numpy as np

x = pd.Series([10, 11, 12, 9, 13, 12, 10])

print("The Series contains:")
print(x)

print("\nx.transform(lambda x: x*10) returns:")
print(x.transform(lambda x: x*10))

The output of the above code will be:

The Series contains:
0    10
1    11
2    12
3     9
4    13
5    12
6    10
dtype: int64

x.transform(lambda x: x*10) returns:
0    100
1    110
2    120
3     90
4    130
5    120
6    100
dtype: int64

Example: using more operations on a Series

Multiple operations can be applied on a Series at the same time. Like in the example below, two operations - 'sqrt' (square root) and 'cbrt' (cube root) are applied, each producing the Series of same length.

import pandas as pd
import numpy as np

x = pd.Series([10, 11, 12, 9, 13, 12, 10])

print("The Series is:")
print(x)

print("\nx.transform(['sqrt', 'cbrt']) returns:")
print(x.transform(['sqrt', 'cbrt']))

The output of the above code will be:

The Series is:
0    10
1    11
2    12
3     9
4    13
5    12
6    10
dtype: int64

x.transform(['sqrt', 'cbrt']) returns:
       sqrt      cbrt
0  3.162278  2.154435
1  3.316625  2.223980
2  3.464102  2.289428
3  3.000000  2.080084
4  3.605551  2.351335
5  3.464102  2.289428
6  3.162278  2.154435

Example: using transform() on selected series in a DataFrame

Similarly, the transform() function can be applied on selected series/column of a given DataFrame. Consider the following example.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Sample1": [1, 2, 3, 4, 5],
  "Sample2": [11, 12, 13, 14, 15],
  "Sample3": [21, 22, 23, 24, 25]},
  index= ["x1", "x2", "x3", "x4", "x5"]
)

print("The DataFrame contains:")
print(df)

#applying transform() on 'Sample2' series
print("\ndf['Sample2'].transform('sqrt') returns:")
print(df['Sample2'].transform('sqrt'))

The output of the above code will be:

The DataFrame contains:
    Sample1  Sample2  Sample3
x1        1       11       21
x2        2       12       22
x3        3       13       23
x4        4       14       24
x5        5       15       25

df['Sample2'].transform('sqrt') returns:
x1    3.316625
x2    3.464102
x3    3.605551
x4    3.741657
x5    3.872983
Name: Sample2, dtype: float64

❮ Pandas Series - Functions

5