Python pandas.core.frame.DataFrame.apply() Examples

The following are 30 code examples of pandas.core.frame.DataFrame.apply(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pandas.core.frame.DataFrame , or try the search function .
Example #1
Source Project: recruit   Author: Frank-qlu   File: frame.py    License: Apache License 2.0 6 votes vote down vote up
def cumsum(self, axis=0, *args, **kwargs):
        """
        Return SparseDataFrame of cumulative sums over requested axis.

        Parameters
        ----------
        axis : {0, 1}
            0 for row-wise, 1 for column-wise

        Returns
        -------
        y : SparseDataFrame
        """
        nv.validate_cumsum(args, kwargs)

        if axis is None:
            axis = self._stat_axis_number

        return self.apply(lambda x: x.cumsum(), axis=axis) 
Example #2
Source Project: recruit   Author: Frank-qlu   File: frame.py    License: Apache License 2.0 6 votes vote down vote up
def applymap(self, func):
        """
        Apply a function to a DataFrame that is intended to operate
        elementwise, i.e. like doing map(func, series) for each series in the
        DataFrame

        Parameters
        ----------
        func : function
            Python function, returns a single value from a single value

        Returns
        -------
        applied : DataFrame
        """
        return self.apply(lambda x: lmap(func, x)) 
Example #3
Source Project: recruit   Author: Frank-qlu   File: groupby.py    License: Apache License 2.0 6 votes vote down vote up
def shift(self, periods=1, freq=None, axis=0, fill_value=None):
        """
        Shift each group by periods observations.

        Parameters
        ----------
        periods : integer, default 1
            number of periods to shift
        freq : frequency string
        axis : axis to shift, default 0
        fill_value : optional

            .. versionadded:: 0.24.0
        """

        if freq is not None or axis != 0 or not isna(fill_value):
            return self.apply(lambda x: x.shift(periods, freq,
                                                axis, fill_value))

        return self._get_cythonized_result('group_shift_indexer',
                                           self.grouper, cython_dtype=np.int64,
                                           needs_ngroups=True,
                                           result_is_index=True,
                                           periods=periods) 
Example #4
Source Project: recruit   Author: Frank-qlu   File: series.py    License: Apache License 2.0 6 votes vote down vote up
def aggregate(self, func, axis=0, *args, **kwargs):
        # Validate the axis parameter
        self._get_axis_number(axis)
        result, how = self._aggregate(func, *args, **kwargs)
        if result is None:

            # we can be called from an inner function which
            # passes this meta-data
            kwargs.pop('_axis', None)
            kwargs.pop('_level', None)

            # try a regular apply, this evaluates lambdas
            # row-by-row; however if the lambda is expected a Series
            # expression, e.g.: lambda x: x-x.quantile(0.25)
            # this will fail, so we can try a vectorized evaluation

            # we cannot FIRST try the vectorized evaluation, because
            # then .agg and .apply would have different semantics if the
            # operation is actually defined on the Series, e.g. str
            try:
                result = self.apply(func, *args, **kwargs)
            except (ValueError, AttributeError, TypeError):
                result = func(self, *args, **kwargs)

        return result 
Example #5
Source Project: vnpy_crypto   Author: birforce   File: frame.py    License: MIT License 6 votes vote down vote up
def cumsum(self, axis=0, *args, **kwargs):
        """
        Return SparseDataFrame of cumulative sums over requested axis.

        Parameters
        ----------
        axis : {0, 1}
            0 for row-wise, 1 for column-wise

        Returns
        -------
        y : SparseDataFrame
        """
        nv.validate_cumsum(args, kwargs)

        if axis is None:
            axis = self._stat_axis_number

        return self.apply(lambda x: x.cumsum(), axis=axis) 
Example #6
Source Project: vnpy_crypto   Author: birforce   File: frame.py    License: MIT License 6 votes vote down vote up
def applymap(self, func):
        """
        Apply a function to a DataFrame that is intended to operate
        elementwise, i.e. like doing map(func, series) for each series in the
        DataFrame

        Parameters
        ----------
        func : function
            Python function, returns a single value from a single value

        Returns
        -------
        applied : DataFrame
        """
        return self.apply(lambda x: lmap(func, x)) 
Example #7
Source Project: predictive-maintenance-using-machine-learning   Author: awslabs   File: frame.py    License: Apache License 2.0 6 votes vote down vote up
def cumsum(self, axis=0, *args, **kwargs):
        """
        Return SparseDataFrame of cumulative sums over requested axis.

        Parameters
        ----------
        axis : {0, 1}
            0 for row-wise, 1 for column-wise

        Returns
        -------
        y : SparseDataFrame
        """
        nv.validate_cumsum(args, kwargs)

        if axis is None:
            axis = self._stat_axis_number

        return self.apply(lambda x: x.cumsum(), axis=axis) 
Example #8
Source Project: predictive-maintenance-using-machine-learning   Author: awslabs   File: frame.py    License: Apache License 2.0 6 votes vote down vote up
def applymap(self, func):
        """
        Apply a function to a DataFrame that is intended to operate
        elementwise, i.e. like doing map(func, series) for each series in the
        DataFrame

        Parameters
        ----------
        func : function
            Python function, returns a single value from a single value

        Returns
        -------
        applied : DataFrame
        """
        return self.apply(lambda x: lmap(func, x)) 
Example #9
Source Project: predictive-maintenance-using-machine-learning   Author: awslabs   File: groupby.py    License: Apache License 2.0 6 votes vote down vote up
def shift(self, periods=1, freq=None, axis=0, fill_value=None):
        """
        Shift each group by periods observations.

        Parameters
        ----------
        periods : integer, default 1
            number of periods to shift
        freq : frequency string
        axis : axis to shift, default 0
        fill_value : optional

            .. versionadded:: 0.24.0
        """

        if freq is not None or axis != 0 or not isna(fill_value):
            return self.apply(lambda x: x.shift(periods, freq,
                                                axis, fill_value))

        return self._get_cythonized_result('group_shift_indexer',
                                           self.grouper, cython_dtype=np.int64,
                                           needs_ngroups=True,
                                           result_is_index=True,
                                           periods=periods) 
Example #10
Source Project: predictive-maintenance-using-machine-learning   Author: awslabs   File: series.py    License: Apache License 2.0 6 votes vote down vote up
def aggregate(self, func, axis=0, *args, **kwargs):
        # Validate the axis parameter
        self._get_axis_number(axis)
        result, how = self._aggregate(func, *args, **kwargs)
        if result is None:

            # we can be called from an inner function which
            # passes this meta-data
            kwargs.pop('_axis', None)
            kwargs.pop('_level', None)

            # try a regular apply, this evaluates lambdas
            # row-by-row; however if the lambda is expected a Series
            # expression, e.g.: lambda x: x-x.quantile(0.25)
            # this will fail, so we can try a vectorized evaluation

            # we cannot FIRST try the vectorized evaluation, because
            # then .agg and .apply would have different semantics if the
            # operation is actually defined on the Series, e.g. str
            try:
                result = self.apply(func, *args, **kwargs)
            except (ValueError, AttributeError, TypeError):
                result = func(self, *args, **kwargs)

        return result 
Example #11
Source Project: Splunking-Crime   Author: nccgroup   File: series.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def aggregate(self, func, axis=0, *args, **kwargs):
        axis = self._get_axis_number(axis)
        result, how = self._aggregate(func, *args, **kwargs)
        if result is None:

            # we can be called from an inner function which
            # passes this meta-data
            kwargs.pop('_axis', None)
            kwargs.pop('_level', None)

            # try a regular apply, this evaluates lambdas
            # row-by-row; however if the lambda is expected a Series
            # expression, e.g.: lambda x: x-x.quantile(0.25)
            # this will fail, so we can try a vectorized evaluation

            # we cannot FIRST try the vectorized evaluation, becuase
            # then .agg and .apply would have different semantics if the
            # operation is actually defined on the Series, e.g. str
            try:
                result = self.apply(func, *args, **kwargs)
            except (ValueError, AttributeError, TypeError):
                result = func(self, *args, **kwargs)

        return result 
Example #12
Source Project: elasticintel   Author: securityclippy   File: series.py    License: GNU General Public License v3.0 6 votes vote down vote up
def aggregate(self, func, axis=0, *args, **kwargs):
        axis = self._get_axis_number(axis)
        result, how = self._aggregate(func, *args, **kwargs)
        if result is None:

            # we can be called from an inner function which
            # passes this meta-data
            kwargs.pop('_axis', None)
            kwargs.pop('_level', None)

            # try a regular apply, this evaluates lambdas
            # row-by-row; however if the lambda is expected a Series
            # expression, e.g.: lambda x: x-x.quantile(0.25)
            # this will fail, so we can try a vectorized evaluation

            # we cannot FIRST try the vectorized evaluation, becuase
            # then .agg and .apply would have different semantics if the
            # operation is actually defined on the Series, e.g. str
            try:
                result = self.apply(func, *args, **kwargs)
            except (ValueError, AttributeError, TypeError):
                result = func(self, *args, **kwargs)

        return result 
Example #13
Source Project: recruit   Author: Frank-qlu   File: frame.py    License: Apache License 2.0 5 votes vote down vote up
def count(self, axis=0, **kwds):
        if axis is None:
            axis = self._stat_axis_number

        return self.apply(lambda x: x.count(), axis=axis) 
Example #14
Source Project: recruit   Author: Frank-qlu   File: groupby.py    License: Apache License 2.0 5 votes vote down vote up
def __call__(self, *args, **kwargs):
        def f(self):
            return self.plot(*args, **kwargs)
        f.__name__ = 'plot'
        return self._groupby.apply(f) 
Example #15
Source Project: recruit   Author: Frank-qlu   File: groupby.py    License: Apache License 2.0 5 votes vote down vote up
def __getattr__(self, name):
        def attr(*args, **kwargs):
            def f(self):
                return getattr(self.plot, name)(*args, **kwargs)
            return self._groupby.apply(f)
        return attr 
Example #16
Source Project: recruit   Author: Frank-qlu   File: groupby.py    License: Apache License 2.0 5 votes vote down vote up
def _python_apply_general(self, f):
        keys, values, mutated = self.grouper.apply(f, self._selected_obj,
                                                   self.axis)

        return self._wrap_applied_output(
            keys,
            values,
            not_indexed_same=mutated or self.mutated) 
Example #17
Source Project: recruit   Author: Frank-qlu   File: groupby.py    License: Apache License 2.0 5 votes vote down vote up
def describe(self, **kwargs):
        with _group_selection_context(self):
            result = self.apply(lambda x: x.describe(**kwargs))
            if self.axis == 1:
                return result.T
            return result.unstack() 
Example #18
Source Project: recruit   Author: Frank-qlu   File: groupby.py    License: Apache License 2.0 5 votes vote down vote up
def cumprod(self, axis=0, *args, **kwargs):
        """
        Cumulative product for each group.
        """
        nv.validate_groupby_func('cumprod', args, kwargs,
                                 ['numeric_only', 'skipna'])
        if axis != 0:
            return self.apply(lambda x: x.cumprod(axis=axis, **kwargs))

        return self._cython_transform('cumprod', **kwargs) 
Example #19
Source Project: recruit   Author: Frank-qlu   File: groupby.py    License: Apache License 2.0 5 votes vote down vote up
def cumsum(self, axis=0, *args, **kwargs):
        """
        Cumulative sum for each group.
        """
        nv.validate_groupby_func('cumsum', args, kwargs,
                                 ['numeric_only', 'skipna'])
        if axis != 0:
            return self.apply(lambda x: x.cumsum(axis=axis, **kwargs))

        return self._cython_transform('cumsum', **kwargs) 
Example #20
Source Project: recruit   Author: Frank-qlu   File: groupby.py    License: Apache License 2.0 5 votes vote down vote up
def cummax(self, axis=0, **kwargs):
        """
        Cumulative max for each group.
        """
        if axis != 0:
            return self.apply(lambda x: np.maximum.accumulate(x, axis))

        return self._cython_transform('cummax', numeric_only=False) 
Example #21
Source Project: recruit   Author: Frank-qlu   File: groupby.py    License: Apache License 2.0 5 votes vote down vote up
def pct_change(self, periods=1, fill_method='pad', limit=None, freq=None,
                   axis=0):
        """
        Calculate pct_change of each value to previous entry in group.
        """
        if freq is not None or axis != 0:
            return self.apply(lambda x: x.pct_change(periods=periods,
                                                     fill_method=fill_method,
                                                     limit=limit, freq=freq,
                                                     axis=axis))
        filled = getattr(self, fill_method)(limit=limit)
        filled = filled.drop(self.grouper.names, axis=1)
        fill_grp = filled.groupby(self.grouper.labels)
        shifted = fill_grp.shift(periods=periods, freq=freq)
        return (filled / shifted) - 1 
Example #22
Source Project: recruit   Author: Frank-qlu   File: groupby.py    License: Apache License 2.0 5 votes vote down vote up
def head(self, n=5):
        """
        Returns first n rows of each group.

        Essentially equivalent to ``.apply(lambda x: x.head(n))``,
        except ignores as_index flag.

        %(see_also)s

        Examples
        --------

        >>> df = pd.DataFrame([[1, 2], [1, 4], [5, 6]],
                              columns=['A', 'B'])
        >>> df.groupby('A', as_index=False).head(1)
           A  B
        0  1  2
        2  5  6
        >>> df.groupby('A').head(1)
           A  B
        0  1  2
        2  5  6
        """
        self._reset_group_selection()
        mask = self._cumcount_array() < n
        return self._selected_obj[mask] 
Example #23
Source Project: recruit   Author: Frank-qlu   File: groupby.py    License: Apache License 2.0 5 votes vote down vote up
def tail(self, n=5):
        """
        Returns last n rows of each group.

        Essentially equivalent to ``.apply(lambda x: x.tail(n))``,
        except ignores as_index flag.

        %(see_also)s

        Examples
        --------

        >>> df = pd.DataFrame([['a', 1], ['a', 2], ['b', 1], ['b', 2]],
                              columns=['A', 'B'])
        >>> df.groupby('A').tail(1)
           A  B
        1  a  2
        3  b  2
        >>> df.groupby('A').head(1)
           A  B
        0  a  1
        2  b  1
        """
        self._reset_group_selection()
        mask = self._cumcount_array(ascending=False) < n
        return self._selected_obj[mask] 
Example #24
Source Project: vnpy_crypto   Author: birforce   File: frame.py    License: MIT License 5 votes vote down vote up
def count(self, axis=0, **kwds):
        if axis is None:
            axis = self._stat_axis_number

        return self.apply(lambda x: x.count(), axis=axis) 
Example #25
Source Project: vnpy_crypto   Author: birforce   File: series.py    License: MIT License 5 votes vote down vote up
def autocorr(self, lag=1):
        """
        Lag-N autocorrelation

        Parameters
        ----------
        lag : int, default 1
            Number of lags to apply before performing autocorrelation.

        Returns
        -------
        autocorr : float
        """
        return self.corr(self.shift(lag)) 
Example #26
Source Project: predictive-maintenance-using-machine-learning   Author: awslabs   File: frame.py    License: Apache License 2.0 5 votes vote down vote up
def count(self, axis=0, **kwds):
        if axis is None:
            axis = self._stat_axis_number

        return self.apply(lambda x: x.count(), axis=axis) 
Example #27
Source Project: predictive-maintenance-using-machine-learning   Author: awslabs   File: groupby.py    License: Apache License 2.0 5 votes vote down vote up
def __call__(self, *args, **kwargs):
        def f(self):
            return self.plot(*args, **kwargs)
        f.__name__ = 'plot'
        return self._groupby.apply(f) 
Example #28
Source Project: predictive-maintenance-using-machine-learning   Author: awslabs   File: groupby.py    License: Apache License 2.0 5 votes vote down vote up
def __getattr__(self, name):
        def attr(*args, **kwargs):
            def f(self):
                return getattr(self.plot, name)(*args, **kwargs)
            return self._groupby.apply(f)
        return attr 
Example #29
Source Project: predictive-maintenance-using-machine-learning   Author: awslabs   File: groupby.py    License: Apache License 2.0 5 votes vote down vote up
def _python_apply_general(self, f):
        keys, values, mutated = self.grouper.apply(f, self._selected_obj,
                                                   self.axis)

        return self._wrap_applied_output(
            keys,
            values,
            not_indexed_same=mutated or self.mutated) 
Example #30
Source Project: predictive-maintenance-using-machine-learning   Author: awslabs   File: groupby.py    License: Apache License 2.0 5 votes vote down vote up
def describe(self, **kwargs):
        with _group_selection_context(self):
            result = self.apply(lambda x: x.describe(**kwargs))
            if self.axis == 1:
                return result.T
            return result.unstack()