search
Search
Publish
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe: "Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
share
thumb_up_alt
bookmark
arrow_backShare
Twitter
Facebook
thumb_up
0
thumb_down
0
chat_bubble_outline
0
auto_stories new
settings

Pandas DataFrame | transform method

Programming
chevron_right
Python
chevron_right
Pandas
chevron_right
Documentation
chevron_right
DataFrame
chevron_right
Functions and Aggregations
schedule Mar 9, 2022
Last updated
local_offer PythonPandas
Tags

Pandas DataFrame.transform(~) method applies a function to transform the rows or columns of the source DataFrame. Note that a new DataFrame is returned, and the source DataFrame is kept intact.

Parameters

1. func | function or string or list or dict

The transformation applied to the rows or columns of the source DataFrame. If a function is passed, then it takes in as argument a Series or a DataFrame.

The allowed values are as follows:

  • a function (e.g. np.mean)

  • the name of the function as a string (e.g. "np.mean")

  • a list of the above two (e.g. [np.mean, "np.max"])

  • dictionary with:

    • key: row/column label

    • value: function, function name or list of such

2. axis | list | optional

Whether to apply the transformation row-wise or column-wise:

Axis

Description

Transform each column.

0 or "index"

Transform each row.

1 or "columns"

By default, axis=0.

3. args | any

The positional arguments you want to pass to func.

4. kwargs | any

The keyword arguments you want to pass to func.

Return Value

A new DataFrame that has the same shape as the source DataFrame.

Examples

Basic usage

Consider the following DataFrame:

df = pd.DataFrame({"A":[-3,4],"B":[5,-6]})
df
   A   B
0  -3  5
1  4   -6

Applying Numpy's abs(~) method, which returns the absolute value of the input:

df.transform(np.abs)   # or "np.abs"
   A  B
0  3  5
1  4  6

Passing in a function

Consider the following DataFrame:

df = pd.DataFrame({"A":[3,4],"B":[5,6]})
df
   A  B
0  3  5
1  4  6

We can pass in a custom function like so:

def foo(col):
    return 2 * col if np.sum(col) >= 10 else col

df.transform(foo)
   A  B
0  3  10
1  4  12

Here, our custom function foo takes in as argument col, which is a single column of the DataFrame (Series).

Passing in multiple functions

Consider the following DataFrame:

df = pd.DataFrame({"A":[-3,4],"B":[-5,6]})
df
A B
0 -3 -5
1 4 6

To apply multiple transformations, pass in a list like so:

df.transform([np.abs, lambda x: x + 1])
A B
absolute <lambda_0> absolute <lambda_0>
0 3 -2 5 -4
1 4 5 6 7

Notice how the two transformations are independent, that is, both transformations are applied to the original values.

Transforming each column

Consider the following DataFrame:

df = pd.DataFrame({"A":[3,4],"B":[5,6]})
df
   A  B
0  3  5
1  4  6

By default, axis=0, which means that we are transforming each column:

def foo(col):
    return 2 * col if np.sum(col) >= 10 else col

df.transform(foo)   # axis=0
   A  B
0  3  10
1  4  12

Transforming each row

Consider the same DataFrame as before:

df = pd.DataFrame({"A":[3,4],"B":[5,6]})
df
   A  B
0  3  5
1  4  6

To transform each row, pass in axis=1 like so:

def foo(row):   # row is a Series representing a single row
    return 2 * row if np.sum(row) >= 10 else row

df.transform(foo, axis=1)
   A  B
0  3  5
1  8  12

Passing in arguments

Consider the following DataFrame:

df = pd.DataFrame({"A":[3,4],"B":[5,6]})
df
   A  B
0  3  5
1  4  6

To pass in arguments to func:

def foo(x, threshold):
    return 2 * x if np.sum(x) >= threshold else x

df.transform(foo, threshold=10)
   A  B
0  3  10
1  4  12
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Ask a question or leave a feedback...