有人知道为什么pandas / numpy地图比列表理解慢吗? 我以为我可以优化我的代码,用map替换列表推导。由于map不需要列表追加操作。
这是一个测试:
df = pd.DataFrame(range(100000))
列表理解:
%timeit -n 10 df["A"] = [x for x in df[0]]
#10 loops, best of 3: 550 ms per loop
熊猫地图
%timeit -n 10 df["A"] = df[0].map(lambda x: x)
#10 loops, best of 3: 797 ms per loop
更新 基于评论贝尔 - 列表理解和地图调用相同的函数f,列表理解更快
def f(x):
return x
%timeit -n 100 df["A"] = df[0].map(f)
#100 loops, best of 3: 475 ms per loop
%timeit -n 100 df["A"] = [f(x) for x in df[0]]
#100 loops, best of 3: 399 ms per loop
答案 0 :(得分:0)
以下是我的结果:
列表理解:
In [33]: %timeit df["A"] = [x for x in df[0]]
10 loops, best of 3: 72.6 ms per loop
简单的列分配:
In [34]: %timeit df["A"] = df[0]
The slowest run took 5.75 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 661 µs per loop
使用.map()
方法:
In [35]: map_df = pd.Series(np.random.randint(0, 10**6, 100000))
In [36]: %timeit df["A"] = df[0].map(map_df)
10 loops, best of 3: 19.8 ms per loop