0

In my Azure Databricks notebook, I have defined a function fn_myFunction(...) in the code below. But I am getting the following error:

NameError: name 'fn_myFunction' is not defined

Question: What could be a cause of the error, and how can we fix it?

import sqlalchemy as sq
import pandas as pd

pw = dbutils.secrets.get(scope='SomeScope',key='sql')
engine = sq.create_engine('mssql+pymssql://SERVICE.Databricks.NONPUBLICETL:'+pw+'MyAzureSQL.database.windows.net:1433/TEST', isolation_level="AUTOCOMMIT")

app_df = pd.read_sql('select * from MyTable', con=engine)

#create new column
app_df['NewColumn'] = app_df['TestColumn'].apply(lambda x: fn_myFunction(x))
.............
.............

def fn_myFunction(lastname):
    testvar = lastname.lower()
    testvar = testvar.strip()
    
    return testvar
nam
  • 21,967
  • 37
  • 158
  • 332
  • 2
    You need to define your function _before_ you try to use it. In this case it looks like you're trying to call your function, and then later define it – G. Anderson Dec 27 '21 at 23:38
  • 1
    @G.Anderson Your suggestion worked. Thank you for helping this new Python learner. +1. – nam Dec 27 '21 at 23:56

0 Answers0