gpt4 book ai didi

python - 向量化内存函数

转载 作者:行者123 更新时间:2023-11-28 21:37:20 24 4
gpt4 key购买 nike

我有一个约 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仍然比我想要的要花更长的时间。

有什么方法可以使用矢量化来加快这一步吗?或者还有其他不是矢量化的技巧吗?

最佳答案

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

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

关于python - 向量化内存函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49734856/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com