在 Jupyter 中调用定义的函数时出现名称错误
Name error when calling defined function in Jupyter
我正在学习 https://blog.patricktriest.com/analyzing-cryptocurrencies-python/ 上的教程,但有点卡住了。我很想定义一个函数,然后立即调用它。
我的代码如下:
def merge_dfs_on_column(dataframes, labels, col):
'''merge a single column of each dataframe on to a new combined dataframe'''
series_dict={}
for index in range(len(dataframes)):
series_dict[labels[index]]=dataframes[index][col]
return pd.DataFrame(series_dict)
# Merge the BTC price dataseries into a single dataframe
btc_usd_datasets= merge_dfs_on_column(list(exchange_data.values()),list(exchange_data.keys()),'Weighted Price')
我可以清楚地看到我已经定义了 merge_dfs_on_column 函数并且我认为语法是正确的,但是,当我在最后一行调用该函数时,出现以下错误:
NameError Traceback (most recent call last)
<ipython-input-22-a113142205e3> in <module>()
1 # Merge the BTC price dataseries into a single dataframe
----> 2 btc_usd_datasets= merge_dfs_on_column(list(exchange_data.values()),list(exchange_data.keys()),'Weighted Price')
NameError: name 'merge_dfs_on_column' is not defined
我用 Google 搜索了答案并仔细检查了语法,但我不明白为什么调用时无法识别该函数。
好吧,如果你要定义自己,
那么您可能直接从网络上的某个地方复制并粘贴了它,并且它可能包含您可能看不到的字符。
只需键入并使用 pass
来定义该函数并注释掉其他代码,看看它是否正常工作。
在您调用函数之前,Python 解释器未执行您的函数定义。
仔细检查执行的内容和执行时间。在 Jupyter 中,可以 运行 编码输入顺序,这似乎是你不小心做的。 (也许试试 'Run All')
"运行 所有" 都不起作用。
关闭内核并重新启动也无济于事。
如果我写:
def whatever(a):
return a*2
whatever("hallo")
在下一个单元格中,这有效。
我在jupyter notebook中也经常遇到这种问题
但是在用 %%time
替换 %%
之后,错误就解决了。我不知道为什么?
所以,经过一些浏览,我发现这不是 jupyter notenook 问题,而是 ipython 问题
和 here is the issue and also this problem is answered in this Whosebug question
我正在学习 https://blog.patricktriest.com/analyzing-cryptocurrencies-python/ 上的教程,但有点卡住了。我很想定义一个函数,然后立即调用它。
我的代码如下:
def merge_dfs_on_column(dataframes, labels, col):
'''merge a single column of each dataframe on to a new combined dataframe'''
series_dict={}
for index in range(len(dataframes)):
series_dict[labels[index]]=dataframes[index][col]
return pd.DataFrame(series_dict)
# Merge the BTC price dataseries into a single dataframe
btc_usd_datasets= merge_dfs_on_column(list(exchange_data.values()),list(exchange_data.keys()),'Weighted Price')
我可以清楚地看到我已经定义了 merge_dfs_on_column 函数并且我认为语法是正确的,但是,当我在最后一行调用该函数时,出现以下错误:
NameError Traceback (most recent call last)
<ipython-input-22-a113142205e3> in <module>()
1 # Merge the BTC price dataseries into a single dataframe
----> 2 btc_usd_datasets= merge_dfs_on_column(list(exchange_data.values()),list(exchange_data.keys()),'Weighted Price')
NameError: name 'merge_dfs_on_column' is not defined
我用 Google 搜索了答案并仔细检查了语法,但我不明白为什么调用时无法识别该函数。
好吧,如果你要定义自己,
那么您可能直接从网络上的某个地方复制并粘贴了它,并且它可能包含您可能看不到的字符。
只需键入并使用 pass
来定义该函数并注释掉其他代码,看看它是否正常工作。
在您调用函数之前,Python 解释器未执行您的函数定义。
仔细检查执行的内容和执行时间。在 Jupyter 中,可以 运行 编码输入顺序,这似乎是你不小心做的。 (也许试试 'Run All')
"运行 所有" 都不起作用。
关闭内核并重新启动也无济于事。
如果我写:
def whatever(a):
return a*2
whatever("hallo")
在下一个单元格中,这有效。
我在jupyter notebook中也经常遇到这种问题
但是在用 %%time
替换 %%
之后,错误就解决了。我不知道为什么?
所以,经过一些浏览,我发现这不是 jupyter notenook 问题,而是 ipython 问题
和 here is the issue and also this problem is answered in this Whosebug question