【问题标题】:Vectorize Memoized Function向量化记忆函数
【发布时间】:2018-09-18 23:09:33
【问题描述】:

我有一个约 10,000,000 行的数据框,需要对其中一列进行操作。列中唯一值的数量大约低两个数量级,所以目前我正在通过应用记忆函数进行转换。

new = [foo(x) for x in df.column])
index = np.where(new > df.other, new, df.other)
df.set_index(index)

@memoized
def foo(x):
    if x > 0:
        bar = -1
    else:
        bar = 10
    x *= bar
    return x

数据框的绝对大小意味着计算new 仍然需要比我想要的更长的时间。

有没有办法使用 vecorization 来加快这一步?或者任何其他不是矢量化的技巧?

【问题讨论】:

标签: python pandas numpy vectorization


【解决方案1】:

是的,这是一种矢量化方法:

col = df.column # This is based on your code and is assumed to to return
# a column but I think you should use indexing to get a column like df['colname']

cond1 = col > 0
cond2 = ~cond1
col[cond1] = col[cond1] * -1
col[cond2] = col[cond2] * 10

【讨论】:

    猜你喜欢
    • 2012-05-11
    • 2014-06-02
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-04
    • 2021-03-30
    相关资源
    最近更新 更多