Parallel version of pandas.DataFrame.apply This mimics the pandas version except for the following: 1. Only ``axis=1`` is supported (and must be specified explicitly). 2. The user should provide output metadata via the `meta` keyword. Parameters ---------
(self, function, *args, meta=no_default, axis=0, **kwargs)
| 3176 | |
| 3177 | @insert_meta_param_description(pad=12) |
| 3178 | def apply(self, function, *args, meta=no_default, axis=0, **kwargs): |
| 3179 | """Parallel version of pandas.DataFrame.apply |
| 3180 | |
| 3181 | This mimics the pandas version except for the following: |
| 3182 | |
| 3183 | 1. Only ``axis=1`` is supported (and must be specified explicitly). |
| 3184 | 2. The user should provide output metadata via the `meta` keyword. |
| 3185 | |
| 3186 | Parameters |
| 3187 | ---------- |
| 3188 | func : function |
| 3189 | Function to apply to each column/row |
| 3190 | axis : {0 or 'index', 1 or 'columns'}, default 0 |
| 3191 | - 0 or 'index': apply function to each column (NOT SUPPORTED) |
| 3192 | - 1 or 'columns': apply function to each row |
| 3193 | $META |
| 3194 | args : tuple |
| 3195 | Positional arguments to pass to function in addition to the array/series |
| 3196 | |
| 3197 | Additional keyword arguments will be passed as keywords to the function |
| 3198 | |
| 3199 | Returns |
| 3200 | ------- |
| 3201 | applied : Series or DataFrame |
| 3202 | |
| 3203 | Examples |
| 3204 | -------- |
| 3205 | >>> import pandas as pd |
| 3206 | >>> import dask.dataframe as dd |
| 3207 | >>> df = pd.DataFrame({'x': [1, 2, 3, 4, 5], |
| 3208 | ... 'y': [1., 2., 3., 4., 5.]}) |
| 3209 | >>> ddf = dd.from_pandas(df, npartitions=2) |
| 3210 | |
| 3211 | Apply a function to row-wise passing in extra arguments in ``args`` and |
| 3212 | ``kwargs``: |
| 3213 | |
| 3214 | >>> def myadd(row, a, b=1): |
| 3215 | ... return row.sum() + a + b |
| 3216 | >>> res = ddf.apply(myadd, axis=1, args=(2,), b=1.5) # doctest: +SKIP |
| 3217 | |
| 3218 | By default, dask tries to infer the output metadata by running your |
| 3219 | provided function on some fake data. This works well in many cases, but |
| 3220 | can sometimes be expensive, or even fail. To avoid this, you can |
| 3221 | manually specify the output metadata with the ``meta`` keyword. This |
| 3222 | can be specified in many forms, for more information see |
| 3223 | ``dask.dataframe.utils.make_meta``. |
| 3224 | |
| 3225 | Here we specify the output is a Series with name ``'x'``, and dtype |
| 3226 | ``float64``: |
| 3227 | |
| 3228 | >>> res = ddf.apply(myadd, axis=1, args=(2,), b=1.5, meta=('x', 'f8')) |
| 3229 | |
| 3230 | In the case where the metadata doesn't change, you can also pass in |
| 3231 | the object itself directly: |
| 3232 | |
| 3233 | >>> res = ddf.apply(lambda row: row + 1, axis=1, meta=ddf) |
| 3234 | |
| 3235 | See Also |