pandas apply

简介:
apply(func, convert_dtype=True, args=(), **kwds) method of pandas.core.series.Series instance
    Invoke function on values of Series. Can be ufunc (a NumPy function
    that applies to the entire Series) or a Python function that only works
    on single values
    
    Parameters
    ----------
    func : function
    convert_dtype : boolean, default True
        Try to find better dtype for elementwise function results. If
        False, leave as dtype=object
    args : tuple
        Positional arguments to pass to function in addition to the value
    Additional keyword arguments will be passed as keywords to the function
    
    Returns
    -------
    y : Series or DataFrame if func returns a Series
    
    See also
    --------
    Series.map: For element-wise operations
    Series.agg: only perform aggregating type operations
    Series.transform: only perform transformating type operations
    
    Examples
    --------
    
    Create a series with typical summer temperatures for each city.
    
    >>> import pandas as pd
    >>> import numpy as np
    >>> series = pd.Series([20, 21, 12], index=['London',
    ... 'New York','Helsinki'])
    >>> series
    London      20
    New York    21
    Helsinki    12
    dtype: int64
    
    Square the values by defining a function and passing it as an
    argument to ``apply()``.
    
    >>> def square(x):
    ...     return x**2
    >>> series.apply(square)
    London      400
    New York    441
    Helsinki    144
    dtype: int64
    
    Square the values by passing an anonymous function as an
    argument to ``apply()``.
    
    >>> series.apply(lambda x: x**2)
    London      400
    New York    441
    Helsinki    144
    dtype: int64
    
    Define a custom function that needs additional positional
    arguments and pass these additional arguments using the
    ``args`` keyword.
    
    >>> def subtract_custom_value(x, custom_value):
    ...     return x-custom_value
    
    >>> series.apply(subtract_custom_value, args=(5,))
    London      15
    New York    16
    Helsinki     7
    dtype: int64
    
    Define a custom function that takes keyword arguments
    and pass these arguments to ``apply``.
    
    >>> def add_custom_values(x, **kwargs):
    ...     for month in kwargs:
    ...         x+=kwargs[month]
    ...         return x
    
    >>> series.apply(add_custom_values, june=30, july=20, august=25)
    London      95
    New York    96
    Helsinki    87
    dtype: int64
    
    Use a function from the Numpy library.
    
    >>> series.apply(np.log)
    London      2.995732
    New York    3.044522
    Helsinki    2.484907
    dtype: float64
目录
相关文章
|
数据处理 Python
Pandas数据处理 | apply() 函数用法指南!
本文介绍一下关于 Pandas 中 apply() 函数的几个常见用法,apply() 函数的自由度较高,可以直接对 Series 或者 DataFrame 中元素进行逐元素遍历操作,方便且高效,具有类似于 Numpy 的特性。
|
24天前
|
SQL 数据挖掘 数据处理
不再纠结,一文详解pandas中的map、apply、applymap、groupby、agg...
不再纠结,一文详解pandas中的map、apply、applymap、groupby、agg...
|
4月前
|
NoSQL Serverless Python
在Python的Pandas中,可以通过直接赋值或使用apply函数在DataFrame添加新列。
【5月更文挑战第2天】在Python的Pandas中,可以通过直接赋值或使用apply函数在DataFrame添加新列。方法一是直接赋值,如`df['C'] = 0`,创建新列C并初始化为0。方法二是应用函数,例如定义`add_column`函数计算A列和B列之和,然后使用`df.apply(add_column, axis=1)`,使C列存储每行A、B列的和。
223 0
|
数据采集 数据挖掘 索引
Pandas中第二好用的函数 | 优雅的apply
本文主要讲的是Pandas中第二好用的函数——apply。为什么说第二好用呢?做人嘛,最重要的就是谦虚,做函数也是一样的,而apply就是这样一个优雅而谦虚的函数。我们单独用一篇来为apply树碑立传,原因有二,一是因为apply函数极其灵活高效,甚至是重新定义了pandas的灵活,一旦熟练运用,在数据清洗和分析界可谓是“屠龙在手,天下我有”;二是apply概念相对晦涩,需要结合具体案例去咀嚼和实践。
189 0
Pandas中第二好用的函数 | 优雅的apply
|
数据处理 索引 Python
Python 之 Pandas 处理字符串和apply() 函数、applymap() 函数、map() 函数详解
Python 之 Pandas 处理字符串和apply() 函数、applymap() 函数、map() 函数详解
|
测试技术 索引 Python
Pandas的apply, map, transform介绍和性能测试
在这篇文章中,我们将通过一些示例讨论apply、agg、map和transform的预期用途。
242 0
Pandas的apply, map, transform介绍和性能测试
|
数据挖掘 数据处理 Python
python数据分析-pandas基础(4)-数据映射apply
apply函数的作用:就是用某个指定的函数f来依次作用于DataFrame或者Series的每个数据,可以指定按行处理和按列处理。
300 0
|
Python
pandas100个骚操作:逆天!一行代码让 apply 速度飙到极致
本篇是pandas100个骚操作系列的第 13 篇:一行代码让 pandas 的 apply 速度飙到极致!
pandas100个骚操作:逆天!一行代码让 apply 速度飙到极致
|
Python
一行代码让 pandas 的 apply 速度飙到极致!
前几天,分享了一篇文章,是关于替代pandas的工具vaex。vaex利用了内存映射的原理,所以比pandas能快上几百倍,但是vaex目前功能有限,所以暂时pandas还是无法撼动的。
一行代码让 pandas 的 apply 速度飙到极致!
|
Python
11 pandas DataFrame - 聚合之apply
=== 聚合 apply === 定义:apply是pandas库的一个重要函数,多和groupby函数一起使用,也可以直接用于DataFrame和Series对象。
1553 0