• 怎样解决python dataframe loc,iloc循环处理速度很慢的问题


    怎样解决python dataframe loc,iloc循环处理速度很慢的问题

    1.问题说明

    最近用DataFrame做大数据 处理,发现处理速度特别慢,追究原因,发现是循环处理时,loc,iloc速度都特别慢,当数据量特别大得时候真的是超级慢。查很多资料,发现没有详细说明,以下为解决办法

    2.问题解决

    使用 Pandas.Series.apply 方法,可以对一列数据快速进行处理

    Series.apply(*func*, *convert_dtype=True*, *args=()*, ***kwds*)

    函数说明:

    To lunch typora from Terminal, you could add

    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
    

    例子讲解

    # 首先导入数据
    >>> 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
    
    # 应用1,把每个值都*2
    >>> def square(x):
    ...     return x**2
    >>> series.apply(square)
    London      400
    New York    441
    Helsinki    144
    dtype: int64
    >>> series.apply(lambda x: x**2)
    London      400
    New York    441
    Helsinki    144
    dtype: int64
    
    # 应用2,相减
    >>> 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
    
    # 使用numpy library中得函数
    >>> series.apply(np.log)
    London      2.995732
    New York    3.044522
    Helsinki    2.484907
    dtype: float64
    

    3.总结

    这样可以快速操作一列数据,不必循环操作每行每列数据,对于大数据处理是非常有用的

  • 相关阅读:
    << 和>> 的计算公式
    死锁面试题(什么是死锁,产生死锁的原因及必要条件)
    SpringBoot的注解:@SpringBootApplication注解 vs @EnableAutoConfiguration+@ComponentScan+@Configuration
    SpringBoot入门-15(springboot配置freemarker使用YML)
    shiro 登录
    springMVC RedirectAttributes
    IDEA3.5最新版激活码
    求递归算法时间复杂度:递归树
    渐进复杂度
    PL/SQL注册码
  • 原文地址:https://www.cnblogs.com/gaoss/p/7657044.html
Copyright © 2020-2023  润新知