加速python应用行明智的功能

时间:2017-07-13 12:00:22

标签: python python-2.7 pandas apply

我正在开发一个数据清理项目,我必须清理一个pandas数据框的多个字段作为其中的一部分。我主要是编写正则表达式和简单函数。以下示例,

def func1(s):
    s = str(s)
    s = s.replace(' ', '')
    if len(s) > 0 and s != '0':
        if s.isalpha() and len(s) < 2:
            return s

def func2(s):
    s = str(s)
    s = s.replace(' ', '')
    s = s.strip(whitespace+','+'-'+'/'+'\\')
    if s != '0':
        if s.isalnum() or s.isdigit():
            return s

def func3(s):
    s = str(s)
    if s.isdigit() and s != '0':
        return s
    else:
        return None

def func4(s):
    if str(s['j']).isalpha() and str(s['k']).isdigit() and s['l'] is none:
        return s['k']

并像这样打电话给他们。

x['a'] = x['b'].apply(lambda x: func1(x) if pd.notnull(x) else x)
x['c'] = x['d'].apply(lambda x: func2(x) if pd.notnull(x) else x)
x['e'] = x['f'].apply(lambda x: func3(x) if pd.notnull(x) else x)
x['g'] = x.apply(lambda x: func4(x), axis = 1)

这里的一切都很好,但是我写了近50个这样的函数,我的数据集有超过1000万条记录。脚本运行了几个小时,如果我的理解是正确的,那么函数被称为行,因此每个函数被调用的次数与行一样多,并且需要很长时间来处理它。有没有办法优化这个?我怎样才能以更好的方式解决这个问题?可能不是通过申请功能?感谢。

样本数据集: -

        Name                               f    j    b
339043  Moir Point RD                      3    0   
21880   Fisher-Point Drive Freemans Ba     6    0   
457170  Whakamoenga Point                 29    0   
318399  Motukaraka Point RD                0    0   
274047  Apirana Avenue Point England     360    0   366
207588  Hobsonville Point RD             127    0   
747136  Dog Point RD                     130    0   
325704  Aroha Road Te Arai Point          36    0   
291888  One Tree Point RD                960    0   
207954  Hobsonville Point RD             160    0   205D
248410  Huia Road Point Chevalier        106    0   

2 个答案:

答案 0 :(得分:2)

一般情况下,您应该避免在.apply上致电DataFrame。这真的是什么让你。在幕后,它为Series中的每一行创建一个新的DataFrame,并将其发送到传递给.apply的函数。毋庸置疑,每行开销相当大,因此.apply处于完整DataFrame的速度很慢。

在下面的例子中,我重命名了函数调用中的一些列,因为示例数据是有限的。

import sys
import time
import contextlib
import pandas as pd

@contextlib.contextmanager
def timethis(label):
    '''A context manager to time a bit of code.'''
    print('Timing', label, end=': ')
    sys.stdout.flush()
    start = time.time()
    yield
    print('{:.4g} seconds'.format(time.time() - start))

... func1, func2, and func3 definitions...

def func4(s):
    if str(s['j']).isalpha() and str(s['f']).isdigit() and s['b'] is none:
        return s['f']

x = pd.DataFrame({'f': [3, 6, 29, 0, 360, 127, 130, 36, 960, 160, 106],
                  'j': 0,
                  'b': [None, None, None, None, 366, None, None, None, None, '205D', None]})
x = pd.concat(x for _ in range(100000))
y = x.copy()

x['a'] = x['b'].apply(lambda x: func1(x) if pd.notnull(x) else x)
x['c'] = x['j'].apply(lambda x: func2(x) if pd.notnull(x) else x)
x['e'] = x['f'].apply(lambda x: func3(x) if pd.notnull(x) else x)
with timethis('func4'):
    x['g'] = x.apply(func4, axis = 1)  # The lambda in your example was not needed

...

def vectorized_func4(df):
    '''Accept the whole DataFrame and not just a single row.'''
    j_isalpha = df['j'].astype(str).str.isalpha()
    f_isdigit = df['f'].astype(str).str.isdigit()
    b_None = df['b'].isnull()
    ret_col = df['f'].copy()
    keep_rows = j_isalpha & f_isdigit & b_None
    ret_col[~keep_rows] = None
    return ret_col

y['a'] = vectorized_func1(y['b'])
y['c'] = vectorized_func2(y['j'])
y['e'] = vectorized_func3(y['f'])
with timethis('vectorized_func4'):
    y['g'] = vectorized_func4(y)

输出:

Timing func4: 115.9 seconds
Timing vectorized_func4: 25.09 seconds

事实证明,对于func1func2func3,与矢量化方法相比,它是一种性能。 .apply上的.map(和Series)并不是很慢,因为每个元素没有额外的开销。但是,意味着您应该在.apply时使用Series,而不是调查Series的矢量化内置方法 - 更多通常情况下,你可能比apply做得更好。

以下是如何重写func3进行矢量化的方法(我添加了时间语句,以便我们可以看到花费的时间最多)。

def vectorized_func3(col):
    with timethis('fillna'):
        col = col.fillna('')
    with timethis('astype'):
        col = col.astype(str)
    with timethis('rest'):
        is_digit_string = col.str.isdigit()
        not_0_string = col != '0'
        keep_rows = is_digit_string & not_0_string
        col[~keep_rows] = None
    return col

以下是与func3比较的时间安排:

Timing func3: 8.302 seconds
Timing fillna: 0.006584 seconds
Timing astype: 9.445 seconds
Timing rest: 1.65 seconds

更改dtype的{​​{1}}需要很长时间,因为必须创建新的Series,然后才会转换每个元素。其他一切都是炽热的。如果您可以将算法更改为不需要将数据类型更改为Series,或者可以简单地将其存储为str,那么矢量化方法将更快 (尤其是str)。

<强>外卖

  • 除非绝对必要,否则请不要在vectorized_func4上使用.apply。{li>如果你认为你必须,去喝一杯咖啡,并考虑一下十分钟,并尝试在没有DataFrame的情况下想办法。
  • 尽量不要在.apply上使用.apply,你可以做得更好,但它不会像完整的Series一样糟糕。
  • 尝试提出一种不需要不断转换DataFrame
  • 的算法

答案 1 :(得分:0)

如果..elif在一个具有所有条件的函数中,则可以使用if而不是多个函数。只是一个想法!

相关问题