eval方法可以直接利用c语言的速度,而不用分配中间数组,不需要中间内存的占用.

如果包含多个步骤,每个步骤都要分配一块内存

import numpy as np
import pandas as pd
import timeit


df = pd.DataFrame({'a': np.random.randn(10000000),
'b': np.random.randn(10000000),
'c': np.random.randn(10000000),
'x': 'x'})
# print df
start_time = timeit.default_timer()
df['a']/( df['b']+0.1)-df['c']
end_time = timeit.default_timer()
print (end_time - start_time)
print "___________________"
start_time = timeit.default_timer()
pd.eval("df['a']/( df['b']+0.1)-df['c']")
end_time = timeit.default_timer(http://www.my516.com)
print (end_time - start_time)
运行时间对比 

0.136633455546
___________________
0.087637596342
As of version 0.13 (released January 2014), Pandas includes some experimental tools that allow you to directly access C-speed operations without costly allocation of intermediate arrays.
---------------------

相关文章:

  • 2022-03-06
  • 2022-02-02
  • 2021-07-22
  • 2021-05-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-02
猜你喜欢
  • 2022-12-23
  • 2021-11-06
  • 2021-07-24
  • 2022-01-04
  • 2022-12-23
相关资源
相似解决方案