我有一个 df 定义,我正在成功地运行操作。我想计算迭代for循环和矢量化操作之间的差异。我已经阅读了有关如何使用 timeit 的各种示例,但是当我尝试使用它们时,出现以下错误。我究竟做错了什么?进口:import h5pyimport pandas as pdimport timeit这个循环有效:for u in df['owner'].unique(): print(u, ': ', len(df[(df['owner'] == u)]), sep = '')但是当我尝试这样计时时......:s = """\for u in df['owner'].unique(): print(u, ': ', len(df[(df['owner'] == u)]), sep = '')"""time_iter_1_1_1 = timeit.timeit(s)...它产生这个错误:---------------------------------------------------------------------------NameError Traceback (most recent call last)<ipython-input-34-7526e96d565c> in <module>() 3 # print(u, ': ', len(df[(df['owner'] == u)]), sep = '')""") 4 ----> 5 time_iter_1_1_1 = timeit.timeit(s)~\Anaconda2\envs\py36\lib\timeit.py in timeit(stmt, setup, timer, number, globals) 231 number=default_number, globals=None): 232 """Convenience function to create Timer object and call timeit method."""--> 233 return Timer(stmt, setup, timer, globals).timeit(number) 234 235 def repeat(stmt="pass", setup="pass", timer=default_timer,~\Anaconda2\envs\py36\lib\timeit.py in timeit(self, number) 176 gc.disable() 177 try:--> 178 timing = self.inner(it, self.timer) 179 finally: 180 if gcold:~\Anaconda2\envs\py36\lib\timeit.py in inner(_it, _timer)NameError: name 'df' is not defined当我尝试这个时......:time_iter_1_1_1 = timeit.timeit("""for u in df['owner'].unique(): print(u, ': ', len(df[(df['owner'] == u)]), sep = '')""")...我收到此错误:ERROR:root:An unexpected error occurred while tokenizing inputThe following traceback may be corrupted or invalidThe error message is: ('EOF in multi-line string', (1, 57))...NameError: name 'df' is not defineddf 已定义并正常工作。我怎样才能解决这个问题?
1 回答
Helenr
TA贡献1780条经验 获得超3个赞
有两种选择,要么
传递一个globals允许timeit解析名称的参数,
df = pd.DataFrame(...)
timeit.timeit(statement, globals={'df': df}) # globals=globals()
...或者,传递一个为您setup设置的字符串参数df。
timeit.timeit(statement, setup='import pandas as pd; df = pd.DataFrame(...)')
添加回答
举报
0/150
提交
取消