-3

I'd like help to resolve an issue I run into when I import and try to use a function that is stored in another python file. I am pretty sure it's a basic mistake I am making, and in the other Stackoverflow posts I haven't found anything similar.

The function that I have imported contains some numpy functionality. After importing it, when I call it I get the following error: "NameError: name 'np' is not defined".

Python doesn't seem to know that np represents numpy. But I have imported Numpy as np in both the file that contains the functions as well in the new Jupyter notebook where I want to use the functions. Anybody has any ideas on how to fix this?

The code of the function I am trying to import is:
def find_str_col(df):
str_col=[]
for i in range(0,df.shape[1]):
x = np.sum(df.iloc[:,i].apply(lambda x: isinstance(x,str)))
str_col.append(x)
str_col = np.squeeze(np.where(np.array(str_col)>0))
return str_col

I tried importing the function find_str_col in two ways and both ways don't work:
from Loan_price_utils import *
and also: from Loan_price_utils import find_str_col

If I do copy and paste the actual function in the new workbook it does work.

1 Answers1

0

Declare 'np' as an alias for numpy

import numpy as np

Otherwise 'np' remains an unknown name.

VitaminM
  • 16
  • 2
  • 1
    Doesn't he say he has done that in his question? In either case the OP needs to provide more info. – Peter Featherstone Apr 23 '19 at 07:25
  • Hi Peter, VitaminM, indeed I have declared import numpy as np in both files. Following the request of another stackoverflow user I have added some code. Thanks for helping – Dario Raffaele Apr 23 '19 at 07:59
  • Hi VitaminM, it turns out you were right. I had updated the file "Loan_price_utils" and added "import numpy as np", but the change had not been saved.
    When I properly saved the change and restarted the kernel it worked fine. Thanks
    – Dario Raffaele Apr 23 '19 at 08:11