Transform() Function in Pandas
transform() function is primarily used to apply a function to a DataFrame or Series, typically after a groupby() operation, and return a result that has the same length as the original object.
Maintains original index and length :
It broadcasts the group-wise result back to the original rows.
Applies a single function:
It can take a single function (e.g., a built-in function like 'sum', 'mean', 'max', or a custom function) as an argument.
It adds a column inside the dataframe and broadcast the result to each row.
import pandas as pd
df = Categories
df[‘avg’] = df.groupby(‘Category’)[‘Value’].transform(‘mean’)
df
#transform #function #python #pandas #dataframe #groupby #summary #result #broadcast #each #row #maintaining #original #length #add #new #column #programming
#like #share #subscribe