Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.DataFrame.apply#

DataFrame.apply(func,axis=0,raw=False,result_type=None,args=(),by_row='compat',engine='python',engine_kwargs=None,**kwargs)[source]#

Apply a function along an axis of the DataFrame.

Objects passed to the function are Series objects whose index iseither the DataFrame’s index (axis=0) or the DataFrame’s columns(axis=1). By default (result_type=None), the final return typeis inferred from the return type of the applied function. Otherwise,it depends on theresult_type argument.

Parameters:
funcfunction

Function to apply to each column or row.

axis{0 or ‘index’, 1 or ‘columns’}, default 0

Axis along which the function is applied:

  • 0 or ‘index’: apply function to each column.

  • 1 or ‘columns’: apply function to each row.

rawbool, default False

Determines if row or column is passed as a Series or ndarray object:

  • False : passes each row or column as a Series to thefunction.

  • True : the passed function will receive ndarray objectsinstead.If you are just applying a NumPy reduction function this willachieve much better performance.

result_type{‘expand’, ‘reduce’, ‘broadcast’, None}, default None

These only act whenaxis=1 (columns):

  • ‘expand’ : list-like results will be turned into columns.

  • ‘reduce’ : returns a Series if possible rather than expandinglist-like results. This is the opposite of ‘expand’.

  • ‘broadcast’ : results will be broadcast to the original shapeof the DataFrame, the original index and columns will beretained.

The default behaviour (None) depends on the return value of theapplied function: list-like results will be returned as a Seriesof those. However if the apply function returns a Series theseare expanded to columns.

argstuple

Positional arguments to pass tofunc in addition to thearray/series.

by_rowFalse or “compat”, default “compat”

Only has an effect whenfunc is a listlike or dictlike of funcsand the func isn’t a string.If “compat”, will if possible first translate the func into pandasmethods (e.g.Series().apply(np.sum) will be translated toSeries().sum()). If that doesn’t work, will try call to apply again withby_row=True and if that fails, will call apply again withby_row=False (backward compatible).If False, the funcs will be passed the whole Series at once.

Added in version 2.1.0.

engine{‘python’, ‘numba’}, default ‘python’

Choose between the python (default) engine or the numba engine in apply.

The numba engine will attempt to JIT compile the passed function,which may result in speedups for large DataFrames.It also supports the following engine_kwargs :

  • nopython (compile the function in nopython mode)

  • nogil (release the GIL inside the JIT compiled function)

  • parallel (try to apply the function in parallel over the DataFrame)

    Note: Due to limitations within numba/how pandas interfaces with numba,you should only use this if raw=True

Note: The numba compiler only supports a subset ofvalid Python/numpy operations.

Please read more about thesupported python featuresandsupported numpy featuresin numba to learn what you can or cannot use in the passed function.

Added in version 2.2.0.

engine_kwargsdict

Pass keyword arguments to the engine.This is currently only used by the numba engine,see the documentation for the engine argument for more information.

**kwargs

Additional keyword arguments to pass as keywords arguments tofunc.

Returns:
Series or DataFrame

Result of applyingfunc along the given axis of theDataFrame.

See also

DataFrame.map

For elementwise operations.

DataFrame.aggregate

Only perform aggregating type operations.

DataFrame.transform

Only perform transforming type operations.

Notes

Functions that mutate the passed object can produce unexpectedbehavior or errors and are not supported. SeeMutating with User Defined Function (UDF) methodsfor more details.

Examples

>>>df=pd.DataFrame([[4,9]]*3,columns=['A','B'])>>>df   A  B0  4  91  4  92  4  9

Using a numpy universal function (in this case the same asnp.sqrt(df)):

>>>df.apply(np.sqrt)     A    B0  2.0  3.01  2.0  3.02  2.0  3.0

Using a reducing function on either axis

>>>df.apply(np.sum,axis=0)A    12B    27dtype: int64
>>>df.apply(np.sum,axis=1)0    131    132    13dtype: int64

Returning a list-like will result in a Series

>>>df.apply(lambdax:[1,2],axis=1)0    [1, 2]1    [1, 2]2    [1, 2]dtype: object

Passingresult_type='expand' will expand list-like resultsto columns of a Dataframe

>>>df.apply(lambdax:[1,2],axis=1,result_type='expand')   0  10  1  21  1  22  1  2

Returning a Series inside the function is similar to passingresult_type='expand'. The resulting column nameswill be the Series index.

>>>df.apply(lambdax:pd.Series([1,2],index=['foo','bar']),axis=1)   foo  bar0    1    21    1    22    1    2

Passingresult_type='broadcast' will ensure the same shaperesult, whether list-like or scalar is returned by the function,and broadcast it along the axis. The resulting column names willbe the originals.

>>>df.apply(lambdax:[1,2],axis=1,result_type='broadcast')   A  B0  1  21  1  22  1  2

[8]ページ先頭

©2009-2025 Movatter.jp