I usually do this using zip:

>>> df = pd.DataFrame([[i] for i in range(10)], columns=['num'])
>>> df
    num
0    0
1    1
2    2
3    3
4    4
5    5
6    6
7    7
8    8
9    9

>>> def powers(x):
>>>     return x, x**2, x**3, x**4, x**5, x**6

>>> df['p1'], df['p2'], df['p3'], df['p4'], df['p5'], df['p6'] = \
>>>     zip(*df['num'].map(powers))

>>> df
        num     p1      p2      p3      p4      p5      p6
0       0       0       0       0       0       0       0
1       1       1       1       1       1       1       1
2       2       2       4       8       16      32      64
3       3       3       9       27      81      243     729
4       4       4       16      64      256     1024    4096
5       5       5       25      125     625     3125    15625
6       6       6       36      216     1296    7776    46656
7       7       7       49      343     2401    16807   117649
8       8       8       64      512     4096    32768   262144
9       9       9       81      729     6561    59049   531441
Answer from ostrokach on Stack Overflow
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.apply.html
pandas.DataFrame.apply — pandas 3.0.1 documentation
However if the apply function returns a Series these are expanded to columns. ... Positional arguments to pass to func in addition to the array/series. ... Only has an effect when func is a listlike or dictlike of funcs and the func isn’t a string. If “compat”, will if possible first ...
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.core.window.expanding.Expanding.apply.html
pandas.core.window.expanding.Expanding.apply — pandas 2.3.3 documentation
Calculate the expanding custom aggregation function. ... Must produce a single value from an ndarray input if raw=True or a single value from a Series if raw=False. Can also accept a Numba JIT function with engine='numba' specified. ... False : passes each row or column as a Series to the function. True : the passed function will receive ndarray objects instead. If you are just applying ...
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.expanding.html
pandas.DataFrame.expanding — pandas 3.0.1 documentation
pandas.api.typing.Expanding · An instance of Expanding for further expanding window calculations, e.g. using the sum method. See also · rolling · Provides rolling window calculations. ewm · Provides exponential weighted functions. Notes · See Windowing Operations for further usage details and examples.
🌐
w3resource
w3resource.com › pandas › dataframe › pandas-dataframe-apply.html
pandas-dataframe-apply
Returning a Series inside the function is similar to passing result_type='expand'. The resulting column names will be the Series index.
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 0.25.0 › reference › api › pandas.core.window.Expanding.apply.html
pandas.core.window.Expanding.apply — pandas 0.25.0 documentation
Pandas arrays · Panel · Index objects · Date offsets · Frequencies · Window · Standard moving window functions · Standard expanding window functions · Exponentially-weighted moving window functions · GroupBy · Resampling · Style · Plotting · General utility functions · Extensions · Development · Release Notes · Enter search terms or a module, class or function name. Expanding.apply(self, func, raw=None, args=(), kwargs={})[source]¶ ·
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 0.23 › generated › pandas.DataFrame.apply.html
pandas.DataFrame.apply — pandas 0.23.1 documentation
In the current implementation apply calls func twice on the first column/row to decide whether it can take a fast or slow code path. This can lead to unexpected behavior if func has side-effects, as they will take effect twice for the first column/row. ... Returning a Series inside the function is similar to passing result_type='expand'.
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 0.23.4 › generated › pandas.core.window.Expanding.apply.html
pandas.core.window.Expanding.apply — pandas 0.23.4 documentation
December 7, 2020 - Extending Pandas · Release Notes · Enter search terms or a module, class or function name. Expanding.apply(func, raw=None, args=(), kwargs={})[source]¶ · expanding function apply · See also · pandas.Series.expanding, pandas.DataFrame.expanding · index · modules | next | previous | pandas ...
Find elsewhere
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 0.23 › generated › pandas.core.window.Expanding.apply.html
pandas.core.window.Expanding.apply — pandas 0.23.1 documentation
Extending Pandas · Release Notes · Enter search terms or a module, class or function name. Expanding.apply(func, raw=None, args=(), kwargs={})[source]¶ · expanding function apply · See also · pandas.Series.expanding, pandas.DataFrame.expanding · index · modules | next | previous | pandas ...
Top answer
1 of 2
2

An possible solution is to make the expanding part of the function, and use GroupBy.apply:

def foo1(_df):
    return _df['x1'].expanding().max() * _df['x2'].expanding().apply(lambda x: x[-1], raw=True)

df['foo_result'] = df.groupby('group').apply(foo1).reset_index(level=0, drop=True)
print (df)
  group  time   x1  x2  foo_result
0     A     1   10   1        10.0
3     B     1  100   2       200.0
1     A     2   40   2        80.0
4     B     2  200   0         0.0
2     A     3   30   1        40.0
5     B     3  300   3       900.0

This is not a direct solution to the problem of applying a dataframe function to an expanding dataframe, but it achieves the same functionality.

2 of 2
2

Applying a dataframe function on an expanding window is apparently not possible (at least not for pandas version 0.23.0; EDITED - and also not 1.3.0), as one can see by plugging a print statement into the function.

Running df.groupby('group').expanding().apply(lambda x: bool(print(x)) , raw=False) on the given DataFrame (where the bool around the print is just to get a valid return value) returns:

0    1.0
dtype: float64
0    1.0
1    2.0
dtype: float64
0    1.0
1    2.0
2    3.0
dtype: float64
0    10.0
dtype: float64
0    10.0
1    40.0
dtype: float64
0    10.0
1    40.0
2    30.0
dtype: float64

(and so on - and also returns a dataframe with '0.0' in each cell, of course).

This shows that the expanding window works on a column-by-column basis (we see that first the expanding time series is printed, then x1, and so on), and does not really work on a dataframe - so a dataframe function can't be applied to it.

So, to get the obtained functionality, one would have to put the expanding inside the dataframe function, like in the accepted answer.

🌐
Pandas
pandas.pydata.org › pandas-docs › version › 2.1 › reference › api › pandas.DataFrame.apply.html
pandas.DataFrame.apply — pandas 2.1.4 documentation
However if the apply function returns a Series these are expanded to columns. ... Positional arguments to pass to func in addition to the array/series. ... Only has an effect when func is a listlike or dictlike of funcs and the func isn’t a string. If “compat”, will if possible first ...
🌐
Skytowner
skytowner.com › explore › pandas_dataframe_apply_method
Pandas DataFrame | apply method with Examples
ParametersReturn ValueExamplesApplying ... 100+ top-tier guides Start your free 7-days trial now! Pandas DataFrame.apply(~) applies the specified function to each row or column of the DataFrame....
🌐
Skytowner
skytowner.com › explore › pandas_dataframe_expanding_method
Pandas DataFrame | expanding method with Examples
ParametersReturn ValueExamplesComputing ... with 100+ top-tier guides Start your free 7-days trial now! Pandas DataFrame.expanding(~) method is used to compute cumulative statistics....
🌐
Pandas
pandas.pydata.org › docs › dev › reference › api › pandas.DataFrame.apply.html
pandas.DataFrame.apply — pandas 3.0.0.dev0+2746.g53c5e30e71 documentation
September 8, 2022 - However if the apply function returns a Series these are expanded to columns. ... Positional arguments to pass to func in addition to the array/series. ... Only has an effect when func is a listlike or dictlike of funcs and the func isn’t a string. If “compat”, will if possible first ...
🌐
DataCamp
datacamp.com › tutorial › pandas-apply
Pandas .apply(): What It Does, When It Helps, and Faster Alternatives | DataCamp
October 6, 2025 - Recent pandas releases tightened behavior around .apply() so results are more predictable. List-like returns: In 2.0+, mismatched list lengths raise an error. Use consistent lengths and set result_type='expand' when you want multiple columns.
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 0.23.4 › generated › pandas.DataFrame.apply.html
pandas.DataFrame.apply — pandas 0.23.4 documentation
In the current implementation apply calls func twice on the first column/row to decide whether it can take a fast or slow code path. This can lead to unexpected behavior if func has side-effects, as they will take effect twice for the first column/row. ... Returning a Series inside the function is similar to passing result_type='expand'.
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.DataFrame.expanding.html
pandas.DataFrame.expanding — pandas 3.0.0 documentation
pandas.api.typing.Expanding · An instance of Expanding for further expanding window calculations, e.g. using the sum method. See also · rolling · Provides rolling window calculations. ewm · Provides exponential weighted functions. Notes · See Windowing Operations for further usage details and examples.
🌐
Studytonight
studytonight.com › pandas › pandas-dataframe-expanding-method
Pandas DataFrame expanding() Method - Studytonight
In the below example, the DataFrame.expanding() method calculated the cumulative sum of the selected column in the DataFrame and store the result in the other column. import pandas as pd df = pd.DataFrame({"A": [1, 2, 3],"B": [1, 1, 1]}) print("---The DataFrame is---") print(df) print("------Output of the function is-------") df["result"]=df.A.expanding().sum() print(df)