简体   繁体   中英

Name error when calling defined function in Jupyter

I am following a tutorial over at https://blog.patricktriest.com/analyzing-cryptocurrencies-python/ and I've got a bit stuck. I am tyring to define, then immediately call, a function.

My code is as follows:

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')

I can clearly see that I have defined the merge_dfs_on_column fucntion and I think the syntax is correct, however, when I call the function on the last line, I get the following error:

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

I have Googled for answers and carefully checked the syntax, but I can't see why that function isn't recognised when called.

Your function definition isn't getting executed by the Python interpreter before you call the function.

Double check what is getting executed and when. In Jupyter it's possible to run code out of input-order, which seems to be what you are accidentally doing. (perhaps try 'Run All')

Well, if you're defining yourself,

Then you probably have copy and pasted it directly from somewhere on the web and it might have characters that you are probably not able to see.

Just define that function by typing it and use pass and comment out other code and see if it is working or not.

"run all" does not work.
Shutting down the kernel and restarting does not help either.

If I write:

def whatever(a):
    return a*2

whatever("hallo")

in the next cell, this works.

I have also experienced this kind of problem frequently in jupyter notebook
But after replacing %% with %%time the error resolved. I didn't know why?
So,after some browsing i get that this is not jupyter notenook issue,it is ipython issue
and here is the issue and also this problem is answered in this stackoverflow question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM