You can use apply() method:

import numpy as np
import pandas as pl
np.random.seed(0)

people2 = pd.DataFrame(np.random.randn(5, 5), 
                      columns=['a', 'b', 'c', 'd', 'e'], 
                      index=['Joe', 'Steve', 'Wes', 'Jim', 'Travis'])
key = ['one', 'two', 'one', 'two', 'one']

Grouped = people2.groupby(key)

def f(df):
    df["f"] = (df.a.mean() - df.b.mean())*df.c
    return df

people2 = Grouped.apply(f)
print people2

If you want some generalize method:

Grouped = people2.groupby(key)

def f(a, b, c, **kw):
    return (a.mean() - b.mean())*c

people2["f"] = Grouped.apply(lambda df:f(**df))
print people2
Answer from HYRY on Stack Overflow
๐ŸŒ
Google Groups
groups.google.com โ€บ g โ€บ pydata โ€บ c โ€บ CFrBmyo81CU
data.table like group by transform with multiple columns
Is this something that could be added to the pandas api easily and if so how? ... Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message ... I don't quite get the interest in one-lining everything -- sometimes that makes things clearer, and often it doesn't -- but in your particular case something like ยท In [47]: test["new"] = test.groupby((test.x + test.y) % 2)[["x","y"]].transform(sum).sum(axis=1)
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ user_guide โ€บ groupby.html
Group by: split-apply-combine โ€” pandas 3.0.1 documentation
By using DataFrameGroupBy.ngroup(), we can extract information about the groups in a way similar to factorize() (as described further in the reshaping API) but which applies naturally to multiple columns of mixed type and different sources. This can be useful as an intermediate categorical-like step in processing, when the relationships between the group rows are more important than their content, or as input to an algorithm which only accepts the integer encoding. (For more information about support in pandas for full categorical data, see the Categorical introduction and the API documentation.)
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ pandas โ€บ pandas-group-by-multiple-columns
Pandas Group by Multiple Columns - GeeksforGeeks
July 23, 2025 - To group by multiple columns, you simply pass a list of column names to the groupby() function. ... Consider the following dataset. We will group by Category and Subcategory, and then calculate the sum of the Sales column.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ pandas โ€บ pandas groupby transform
Pandas Groupby Transform - Spark By {Examples}
June 18, 2025 - Pandas groupby transform works on just one Series at a time and groupby apply() works on the entire DataFrame at once. This means you can not access multiple columns while using the groupBy transform function.
๐ŸŒ
Medium
medium.com โ€บ @heyamit10 โ€บ mastering-pandas-groupby-multiple-columns-92411999779f
Mastering pandas groupby Multiple Columns | by Hey Amit | Medium
April 12, 2025 - You might be wondering what makes the groupby function in pandas so special. Simple enough, it lets you group data based on one or more columns, aggregating similar values into summarized forms. This is incredibly useful when working with complex datasets where you need insights derived from grouped data points. ... You might be asking yourself, โ€œWhy not just group by one column?โ€ While grouping by a single column is perfectly fine, juggling multiple columns can give you a finer level of detail and richer insights.
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.DataFrame.groupby.html
pandas.DataFrame.groupby โ€” pandas 3.0.2 documentation
When calling apply and the by argument produces a like-indexed (i.e. a transform) result, add group keys to index to identify pieces. By default group keys are not included when the resultโ€™s index (and column) labels match the inputs, and are included otherwise.
Find elsewhere
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.DataFrame.transform.html
pandas.DataFrame.transform โ€” pandas 3.0.2 documentation
>>> df = pd.DataFrame( ... { ... "c": [1, 1, 1, 2, 2, 2, 2], ... "type": ["m", "n", "o", "m", "m", "n", "n"], ... } ... ) >>> df c type 0 1 m 1 1 n 2 1 o 3 2 m 4 2 m 5 2 n 6 2 n >>> df["size"] = df.groupby("c")["type"].transform(len) >>> df c type size 0 1 m 3 1 1 n 3 2 1 o 3 3 2 m 4 4 2 m 4 5 2 n 4 6 2 n 4
๐ŸŒ
GitHub
github.com โ€บ pandas-dev โ€บ pandas โ€บ issues โ€บ 24267
Feature-Request: Allow lambda function with different columns in transform ยท Issue #24267 ยท pandas-dev/pandas
December 13, 2018 - Currently, if you want to create ... custom function, you have to create intermediate dataframes since transform() cannot work with multiple columns at once. import pandas as pd df = pd.DataFrame({'a':[1,2,3,4,5,6], ...
Author ย  harlecin
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ pandas โ€บ pandas groupby multiple columns explained
Pandas GroupBy Multiple Columns Explained - Spark By {Examples}
June 27, 2025 - How to groupby multiple columns in pandas DataFrame and compute multiple aggregations? groupby() can take the list of columns to group by multiple columns
๐ŸŒ
Datasciencebyexample
datasciencebyexample.com โ€บ 2023 โ€บ 02 โ€บ 14 โ€บ sklearn-columntransformer-one-columns-to-many-columns
Transforming One or More Columns of a Pandas DataFrame using ColumnTransformer | DataScienceTribe
February 14, 2023 - In this blog post, weโ€™ll demonstrate how to use a custom transformer with scikit-learnโ€™s ColumnTransformer to transform one or more columns of a Pandas DataFrame.
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ how-to-simultaneously-melt-multiple-columns-in-python-pandas
How to Simultaneously Melt Multiple Columns in Python Pandas | Saturn Cloud Blog
August 25, 2023 - In this blog post, we will learn how to simultaneously melt multiple columns in Python Pandas. Melting data is the process of transforming wide data into long data by unpivoting columns into rows.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ add-multiple-columns-to-dataframe-in-pandas
Add multiple columns to dataframe in Pandas - GeeksforGeeks
October 3, 2022 - Although insert takes single column name, value as input, but we can use it repeatedly to add multiple columns to the DataFrame. ... # importing pandas library import pandas as pd # creating and initializing a nested list students = [['jackma', 34, 'Sydeny', 'Australia'], ['Ritika', 30, 'Delhi', 'India'], ['Vansh', 31, 'Delhi', 'India'], ['Nany', 32, 'Tokyo', 'Japan'], ['May', 16, 'New York', 'US'], ['Michael', 17, 'las vegas', 'US']] # Create a DataFrame object df = pd.DataFrame(students, columns=['Name', 'Age', 'City', 'Country'], index=['a', 'b', 'c', 'd', 'e', 'f']) # creating columns 'Age' and 'ID' at # 2nd and 3rd position using # dataframe.insert() function df.insert(2, "Marks", [90, 70, 45, 33, 88, 77], True) df.insert(3, "ID", [101, 201, 401, 303, 202, 111], True) # Displaying the Data frame df
๐ŸŒ
Statology
statology.org โ€บ home โ€บ how to convert pandas dataframe columns to int
How to Convert Pandas DataFrame Columns to int
November 28, 2022 - This tutorial explains how to convert pandas DataFrame columns to integer type, including several examples.
๐ŸŒ
GitHub
github.com โ€บ pandas-dev โ€บ pandas โ€บ issues โ€บ 342
Enable easier transformations of multiple columns in DataFrame ยท Issue #342 ยท pandas-dev/pandas
November 7, 2011 - things like df[cols] = transform(df[cols]) should be possible in a mixed-type DataFrmae, per the mailing list discussion
Author ย  wesm