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

Difference between methods apply and transform for groupby in Pandas

Programming
chevron_right
Python
chevron_right
Pandas
chevron_right
Cookbooks
chevron_right
DataFrame Cookbooks
chevron_right
Data Aggregation Cookbook
schedule Mar 9, 2022
Last updated
local_offer PythonPandas
Tags
tocTable of Contents
expand_more

The main differences are the input and output of the argument function:

Input

Output

A scalar, a sequence or a DataFrame.

A DataFrame representing each group.

apply(~)

A Series representing a column of each group.

A sequence that has the same length as the input Series. Scalars will be broadcasted to become a sequence.

transform(~)

What this means is that apply(~) allows you perform operations on columns, rows and the entire DataFrame of each group, whereas transform(~) is restricted to operations on individual columns of each group.

Examples

Difference in input

Consider the following DataFrame:

df = pd.DataFrame({"A":[2,5,4],"B":[10,100,8],"group":["a","a","b"]})
df
A B group
0 2 10 a
1 5 100 a
2 4 8 b

To compute the cumulative sum of rows of each group, you must use apply():

# my_df is a DataFrame representing each group
def f(my_df):
# returns a DataFrame
return my_df.cumsum(axis=1)

df.groupby("group").apply(f)
A B
0 2 12
1 5 105
2 4 12

Here, our function f is called twice - once for each group. Here, transform(f) would not work because transform(f) only allows for operations involving individual columns, and so row operations are not allowed.

To compute the cumulative sum of columns of each group, you can use transform(f):

# my_col is a Series representing a single column of each group
def f(my_col):
# returns a Series
return my_col.cumsum()

df.groupby("group").transform(f)
A B
0 2 10
1 7 110
2 4 8

Here, our function f is called 4 times since we have two groups and each group we have two columns.

NOTE

In most cases, using apply(f) instead of transform(f) would produce identical results since many of the DataFrame's operations, including cumsum(~), are performed for each column by default.

Difference in output

Consider the same DataFrame as before:

df = pd.DataFrame({"A":[2,5,4],"B":[10,100,8],"group":["a","a","b"]})
df
A B group
0 2 10 a
1 5 100 a
2 4 8 b

Returning a scalar for apply(~) yields:

def f(my_df):
# return the maximum value (scalar) in the entire my_df for each group
return my_df.max().max()

df.groupby("group").apply(f) # returns a Series
group
a 100
b 8
dtype: int64

Returning a scalar for transform(~) yields:

# my_col is a Series representing a single column of each group
def f(my_col):
# maximum value (scalar) in column gets broadcasted to become a Series of the same length as my_col
return my_col.max()

df.groupby("group").transform(f) # returns a DataFrame
A B
0 5 100
1 5 100
2 4 8
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...