maxframe.liteframe.LiteFrame.apply#
- LiteFrame.apply(func, axis=0, *, result_type=None, dtypes=None, skip_infer=False, input_frame_type='pandas', args=(), **kwargs)#
Apply
funcper column (axis=0) or per row (axis=1).Mirrors
pandas.DataFrame.apply:axis=0:funcreceives each column as a Series and may return a scalar, Series, or DataFrame. All row chunks are concatenated first so the UDF sees the whole column.axis=1:funcreceives each row as a Series and may return a scalar, Series, or dict. Results are combined one output row per input row. Because per-row UDFs are not mock-inferable from the whole frame,axis=1generally requires explicitdtypesor a return type annotation.
- Parameters:
func (callable or callable class) –
Function taking a Series (a column for
axis=0, a row foraxis=1) and returning a scalar, Series, dict, or DataFrame.If
funcis a class, it is instantiated once per chunk,__call__is invoked per column (axis=0) or per row (axis=1), andclose()(if defined) is always called at the end of the chunk — even when__call__receives single rows/columns.axis ({0, 1}, default 0) –
axis=0iterates over columns;axis=1iterates over rows.result_type ({"expand", "reduce", "broadcast", None}, default None) –
Only applies when
axis=1. Controls how list-like results (dict, list, Series) are combined into the output frame:None(default):Seriesresults are auto-expanded to columns (matching pandas, where a per-rowSeriesis combined into a multi-column frame).dictandlistresults are kept as a single object column (one cell per row) — pandas 2.3.3 does not expand dict keys or lists unlessresult_type='expand'is given; declaring multiple columns for a dict / list result raises."expand": dict / list / Series results are always expanded to columns (dict keys / positional indices / Series index become column names)."reduce": dict / list results are kept as a single column of objects (one cell per row), matching pandasresult_type='reduce'.Seriesresults are still expanded to columns (pandas behavior)."broadcast": results are broadcast to the original input shape — the output has the same column names as the input. Requires list-like results whose length matches the input column count.
dtypes (dict or pd.Series, optional) – Expected output dtypes. If omitted, inferred from
func’s return annotation or a mock call.skip_infer (bool, default False) – If True, skip dtype inference and produce an untyped output.
input_frame_type ({"pandas", "polars"}, default "pandas") – Frame type passed to
func.args (tuple) – Extra positional arguments forwarded to
func.**kwargs – Extra keyword arguments forwarded to
func.
- Returns:
A new LiteFrame whose dtypes match the (inferred or explicit) output schema.
- Return type:
See also
apply_chunkApply a function per execution chunk.
transformTransform per column or per row.
mapApply a function element-wise.
Examples
>>> import maxframe.liteframe as mlf >>> lf = mlf.LiteFrame({'a': [1, 2, 3], 'b': [10, 20, 30]})
Apply a reduction per column (
axis=0, default) —funcreceives each column as a Series:>>> result = lf.apply(lambda col: col.sum()) >>> result.execute() a b 0 6 60
Apply per row (
axis=1) —funcreceives each row as a Series. Declare the output schema withdtypes:>>> result = lf.apply(lambda row: {'sum': row['a'] + row['b']}, ... axis=1, dtypes={'sum': int}) >>> result.execute() sum 0 11 1 22 2 33
With
axis=1andresult_type='expand', dict results are expanded to columns:>>> result = lf.apply(lambda row: {'a2': row['a'] ** 2, 'b2': row['b'] ** 2}, ... axis=1, result_type='expand', ... dtypes={'a2': int, 'b2': int}) >>> result.execute() a2 b2 0 1 100 1 4 400 2 9 900