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)
| 3212 | |
| 3213 | @insert_meta_param_description(pad=12) |
| 3214 | def apply(self, function, *args, meta=no_default, axis=0, **kwargs): |
| 3215 | """Parallel version of pandas.DataFrame.apply |
| 3216 | |
| 3217 | This mimics the pandas version except for the following: |
| 3218 | |
| 3219 | 1. Only ``axis=1`` is supported (and must be specified explicitly). |
| 3220 | 2. The user should provide output metadata via the `meta` keyword. |
| 3221 | |
| 3222 | Parameters |
| 3223 | ---------- |
| 3224 | func : function |
| 3225 | Function to apply to each column/row |
| 3226 | axis : {0 or 'index', 1 or 'columns'}, default 0 |
| 3227 | - 0 or 'index': apply function to each column (NOT SUPPORTED) |
| 3228 | - 1 or 'columns': apply function to each row |
| 3229 | $META |
| 3230 | args : tuple |
| 3231 | Positional arguments to pass to function in addition to the array/series |
| 3232 | |
| 3233 | Additional keyword arguments will be passed as keywords to the function |
| 3234 | |
| 3235 | Returns |
| 3236 | ------- |
| 3237 | applied : Series or DataFrame |
| 3238 | |
| 3239 | Examples |
| 3240 | -------- |
| 3241 | >>> import pandas as pd |
| 3242 | >>> import dask.dataframe as dd |
| 3243 | >>> df = pd.DataFrame({'x': [1, 2, 3, 4, 5], |
| 3244 | ... 'y': [1., 2., 3., 4., 5.]}) |
| 3245 | >>> ddf = dd.from_pandas(df, npartitions=2) |
| 3246 | |
| 3247 | Apply a function to row-wise passing in extra arguments in ``args`` and |
| 3248 | ``kwargs``: |
| 3249 | |
| 3250 | >>> def myadd(row, a, b=1): |
| 3251 | ... return row.sum() + a + b |
| 3252 | >>> res = ddf.apply(myadd, axis=1, args=(2,), b=1.5) # doctest: +SKIP |
| 3253 | |
| 3254 | By default, dask tries to infer the output metadata by running your |
| 3255 | provided function on some fake data. This works well in many cases, but |
| 3256 | can sometimes be expensive, or even fail. To avoid this, you can |
| 3257 | manually specify the output metadata with the ``meta`` keyword. This |
| 3258 | can be specified in many forms, for more information see |
| 3259 | ``dask.dataframe.utils.make_meta``. |
| 3260 | |
| 3261 | Here we specify the output is a Series with name ``'x'``, and dtype |
| 3262 | ``float64``: |
| 3263 | |
| 3264 | >>> res = ddf.apply(myadd, axis=1, args=(2,), b=1.5, meta=('x', 'f8')) |
| 3265 | |
| 3266 | In the case where the metadata doesn't change, you can also pass in |
| 3267 | the object itself directly: |
| 3268 | |
| 3269 | >>> res = ddf.apply(lambda row: row + 1, axis=1, meta=ddf) |
| 3270 | |
| 3271 | See Also |