Pandas DataFrame.transform: Apply a Function to Transform a DataFrame


Pandas DataFrame.transform

The DataFrame.transform method in pandas is used to apply a function to transform the values of a DataFrame. Unlike aggregation, the transformation preserves the shape of the DataFrame, meaning the output has the same dimensions as the input.


Syntax

The syntax for DataFrame.transform is:

DataFrame.transform(func, axis=0, *args, **kwargs)

Here, DataFrame refers to the pandas DataFrame being transformed.


Parameters

ParameterDescription
funcThe function to apply to each column or row of the DataFrame. This can be a function, string, list of functions, or dictionary mapping columns to functions.
axisSpecifies the axis along which the function is applied. Use 0 or 'index' to apply the function to each column, and 1 or 'columns' to apply the function to each row. Defaults to 0.
*argsPositional arguments to pass to the function.
**kwargsKeyword arguments to pass to the function.

Returns

A DataFrame with the same shape as the original, where each element has been transformed by the specified function.


Examples

Applying a Function to Each Column of a DataFrame

This example demonstrates how to use transform to apply a function (e.g., lambda x: x * 2) to each column of a DataFrame.

Python Program

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})

# Apply a transformation function to each column
result = df.transform(lambda x: x * 2)
print(result)

Output

   A   B   C
0  2   8  14
1  4  10  16
2  6  12  18

Applying Multiple Functions to Each Column of a DataFrame

This example shows how to use transform to apply multiple functions (e.g., sqrt and log) to each column of a DataFrame.

Python Program

import pandas as pd
import numpy as np

# Create a DataFrame
df = pd.DataFrame({'A': [1, 4, 9], 'B': [16, 25, 36], 'C': [49, 64, 81]})

# Apply multiple transformation functions to each column
result = df.transform([np.sqrt, np.log])
print(result)

Output

         A                   B                   C
      sqrt       log      sqrt       log      sqrt       log
0  1.000000  0.000000  4.000000  2.772589  7.000000  3.891820
1  2.000000  1.386294  5.000000  3.218876  8.000000  4.158883
2  3.000000  2.197225  6.000000  3.583519  9.000000  4.394449

Applying Different Functions to Different Columns of a DataFrame

This example demonstrates how to use transform with a dictionary to apply different functions to different columns of a DataFrame.

Python Program

import pandas as pd
import numpy as np

# Create a DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})

# Apply different transformation functions to different columns
result = df.transform({'A': np.sqrt, 'B': lambda x: x * 2, 'C': np.log})
print(result)

Output

     A   B         C
0  1.0   8  1.945910
1  1.414214  10  2.079442
2  1.732051  12  2.197225

Applying a Function to Rows of a DataFrame

This example shows how to use transform to apply a function to rows of a DataFrame by setting axis=1.

Python Program

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})

# Apply a transformation function to each row
result = df.transform(lambda x: x + 10, axis=1)
print(result)

Output

    A   B   C
0  11  14  17
1  12  15  18
2  13  16  19

Summary

In this tutorial, we explored the DataFrame.transform method in pandas. Key takeaways include:

  • Using transform to apply a function to each column or row of a DataFrame while preserving its shape.
  • Applying multiple functions or different functions to different columns using lists or dictionaries.
  • Understanding the difference between transform and aggregation methods like agg.

Python Libraries