Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 414
Westworld Avatar asked Jul 22 '26 02:07

Westworld


2 Answers

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

like image 57
David A Avatar answered Jul 25 '26 00:07

David A


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.

like image 45
Muku Avatar answered Jul 25 '26 02:07

Muku



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!