Pandas DataFrame.apply()The Pandas apply() function allows the user to pass a function and apply it to every single value of the Pandas series. This function improves the capabilities of the panda's library because it helps to segregate data according to the conditions required. So that it can be efficiently used for data science and machine learning. The objects that are to be passed to function are Series objects whose index is either the DataFrame's index, i.e., axis=0 or the DataFrame's columns, i.e., axis=1. By default, the result_type=None and the final return type is inferred from the return type of the applied function. Otherwise, it depends on the result_type argument. Syntax:Parameters:- func: It is a function that is to be applied to each column or row.
- axis: {0 or 'index', 1 or 'columns'}, default value 0
It is an axis along which the function is applied. It can have two values:- 0 or 'index': It applies the function to each of the columns.
- 1 or 'columns': It applies the function to each of the rows.
- broadcast: It is an optional parameter that returns the boolean values.
Only relevant for aggregation functions: False or None: It returns a Series whose length will be the length of the index or the number of columns based on the axis parameter. True: The results will be broadcasted to the original shape of the frame; the original index and columns will be retained. - raw: bool, default value False
False: It passes each row or column as a Series to the function. True: The passed function will receive a ndarray objects. If you are applying a NumPy reduction function, it will achieve better performance. - reduce: bool or None, default value None
It tries to apply the reduction procedures. If the DataFrame is empty, the apply will use the reduce to determine whether the result should be a Series or a DataFrame. By default, reduce=None, the apply's return value will be guessed by calling func on an empty Series (note: All the exceptions that are to be raised by func will be ignored while guessing). If reduce=True, Series will always be returned, whereas reduce=False, Always the DataFrame will be returned. - result_type: {'expand', 'reduce', 'broadcast', None}, default value None
These only act when axis=1 (columns): 'expand': It defines the list-like results that will be turned into columns. 'reduce': It is the opposite of 'expand'. If possible, it returns a Series rather than expanding list-like results. 'broadcast': It broadcast the results to the original shape of the DataFrame, the original index, and the columns will be retained. The default value None depends on the return value of the applied function , i.e., list-like results returned as a Series of those. If apply returns a Series, it expands to the columns. - args: It is a positional argument that is to be passed to func in addition to the array/series.
- **kwds: It is an optional keyword argument, which is used to pass as keywords arguments to func.
Returns:It returns the result of applying func along the given axis of the DataFrame. Example:Output A B
0 2 7
1 2 7
2 2 7
3 2 7
|