Why Is Pandas Map Slower Than List Comprehension
Does someone know why pandas/numpy map is slower then list comprehension? I thought I could optimize my code replacing the list comprehensions by map. Since map doesn't need the li
Solution 1:
Here are my results:
list comprehension:
In [33]: %timeit df["A"] = [x for x indf[0]]
10 loops, best of 3: 72.6 ms per loop
simple column assignment:
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
using .map()
method:
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
Post a Comment for "Why Is Pandas Map Slower Than List Comprehension"