Inspired by Jeff's answer. This is the fastest method on my machine:

pd.Series(np.repeat(grp.mean().values, grp.count().values))
Answer from YXD on Stack Overflow
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.transform.html
pandas.DataFrame.transform — pandas 3.0.2 documentation
Call func on self producing a DataFrame with the same axis shape as self · Function to use for transforming the data. If a function, must either work when passed a DataFrame or when passed to DataFrame.apply. If func is both list-like and dict-like, dict-like behavior takes precedence
🌐
W3Schools
w3schools.com › python › pandas › ref_df_transform.asp
Pandas DataFrame transform() Method
import pandas as pd def eur_to_nok(x): return x * 10 data = { "for1": [2, 6, 3], "for5": [8, 20, 12] } df = pd.DataFrame(data) newdf = df.transform(eur_to_nok) print(newdf) Try it Yourself »
🌐
Practical Business Python
pbpython.com › pandas_transform.html
Understanding the Transform Function in Pandas - Practical Business Python
April 4, 2017 - The transform function in pandas can be a useful tool for combining and analyzing data.
🌐
Analytics Vidhya
analyticsvidhya.com › home › learn how to use the transform function in pandas (with python code)
Learn How to use the Transform Function in Pandas (with Python code)
November 25, 2024 - We can solve this effectively using the transform function in Pandas. This is an important function for creating features. Trust me, it can be a game-changer! After performing the transformation, the transform function retains the same number of items as the original dataset. We’ll be leaning on a super-easy one-line step using groupby followed by a transform: df["User_Mean"] = df.groupby('User_ID')["Purchase"].transform('mean')
🌐
Medium
medium.com › @amit25173 › what-is-transform-in-pandas-c5b5a7a81cf2
What is transform() in Pandas?
March 6, 2025 - Element-wise Transformation: Applies functions to each element individually. Shape-Preserving: Returns output with the same structure (rows & columns) as the original data. ... Built-in functions like mean, sum, etc. Custom functions using lambda or defined functions. ... func: The function you want to apply. This can be a built-in function ('mean', 'sum', etc.) or a custom one (lambda functions). ... import pandas as pd # Sample DataFrame data = {'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8]} df = pd.DataFrame(data) # Applying transform to get column-wise mean result = df.transform('mean') print(result)
🌐
Scaler
scaler.com › home › topics › how to use pandas transform() function?
How to use Pandas transform() Function? - Scaler Topics
May 4, 2023 - Now if we want to perform a specific task on all the values like incrementing all the values by a certain digit, or something like that then we can use loops to iterate over each value but this will take a lot of computation and the speed also becomes slow. If the DataFrame is more than two-dimensional then the execution time will increase way more than we think. So, in these scenarios, we use the Pandas transform() function which transforms every value of the DataFrame concerning the provided function.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pandas-dataframe-transform
Python | Pandas DataFrame.transform - GeeksforGeeks
February 21, 2019 - This is the primary data structure of the Pandas. Pandas DataFrame.transform() function call func on self producing a DataFrame with transformed values and that has the same axis length as self.
Find elsewhere
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas groupby transform
Pandas Groupby Transform - Spark By {Examples}
June 18, 2025 - Pandas Groupby Transformed can be performed by using the DataFrameGroupBy.transform() function, this function transforms the DataFrame with the specified function and returns the DataFrame having the same indexes as the original object.
🌐
Medium
medium.com › @amit25173 › understanding-groupby-transform-in-pandas-b59954153907
Understanding groupby().transform() in Pandas | by Amit Yadav | Medium
March 6, 2025 - Z-Score normalization solves this by scaling data relative to each group’s mean and standard deviation. ... import pandas as pd # Sample data data = { 'Store': ['A', 'A', 'B', 'B', 'C', 'C'], 'Sales': [200, 250, 300, 400, 150, 180] } df = pd.DataFrame(data) # Z-score normalization within each store df['Z_Score'] = df.groupby('Store')['Sales'].transform( lambda x: (x - x.mean()) / x.std() ) print(df)
🌐
Towards Data Science
towardsdatascience.com › home › latest › pandas: apply, map or transform?
Pandas: apply, map or transform? | Towards Data Science
January 23, 2025 - Remember that transform must return a dataframe with the same length along the axis it’s applied on. What this means is that even if transform is used with a groupby operation that returns aggregate values, it assigns those aggregate values to each element.
🌐
Statology
statology.org › home › how to use groupby() and transform() functions in pandas
How to Use groupby() and transform() Functions in Pandas
October 19, 2022 - You can use the following methods to use the groupby() and transform() functions together in a pandas DataFrame: Method 1: Use groupby() and transform() with built-in function · df['new'] = df.groupby('group_var')['value_var'].transform('mean')
🌐
Apache
spark.apache.org › docs › latest › api › python › reference › pyspark.pandas › api › pyspark.pandas.groupby.GroupBy.transform.html
pyspark.pandas.groupby.GroupBy.transform — PySpark 4.1.1 documentation
While transform is a very flexible method, its downside is that using it can be quite a bit slower than using more specific methods like agg or transform. pandas-on-Spark offers a wide range of method that will be much faster than using transform for their specific purposes, so try to use them before reaching for transform.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › pandas tutorial › pandas transform
Pandas Transform | How does Transform Function Work in Pandas?
April 1, 2023 - Functions are used to transforming the data. Axis represents 0 for rows or index and 1 for columns and axis considers the value 0 as default. Arguments and keyword arguments help to return the function and produce the output. Change is an activity utilized related to groupby (which is one of the most helpful tasks in pandas).
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Reddit
reddit.com › r/learnpython › is my understanding of pandas .transform() method correct?
r/learnpython on Reddit: Is my understanding of pandas .transform() method correct?
March 1, 2022 -

From what I've learned, all what .transform() does is that it 'stretches' the aggregate function results for every single key value (i.e. the function still performs its stuff like summing up the values, but it won't just return one result per group. Instead for every occurence of a key value in the key column, there will be one result.

If the function is not of aggregating nature, the result seems to be the same whether or not .transform() is used. Is my understanding correct? Or are there some special cases I'm missing?

🌐
Medium
medium.com › @stacymacbrains › when-to-use-pandas-transform-function-e137c89ca070
When to Use Pandas transform() Function | by Ogochukwu Stanley Ikegbo | Medium
January 9, 2025 - The transform() function applies a transformation function element-wise to a DataFrame or Series, returning a DataFrame or Series of the same shape. It's particularly useful in group operations or when modifying columns without changing their ...
🌐
IncludeHelp
includehelp.com › python › transform-vs-aggregate-in-pandas.aspx
Python - Transform vs. aggregate in Pandas
# Importing pandas package import pandas as pd # Importing numpy package import numpy as np # Creating dataframe df = pd.DataFrame(dict(A = list('1122'), B=[1, 2, 3, 4], C=[5, 6, 7, 8])) # Display original DataFrame print("Original DataFrame:\n",df,"\n") # Applying transform tr = df.groupby('A').transform('mean') # Display transform result print("Transform Result:\n",tr,"\n") # Using aggregate ag = df.groupby('A').agg(['mean', 'std']) # Display aggregate result print("Aggregate Result:\n",ag)
🌐
w3resource
w3resource.com › pandas › series › series-transform.php
Pandas: Series - transform() function - w3resource
The transform() function is used to call function on self producing a Series with transformed values and that has the same axis length as self. ... Returns:Series A Series that must have the same length as self.