Why do I get "NameError: name 'df' is not defined"?
Question:
I don’t know why “df” is not defined because i already assigned df a value. I read some question and answers online regarding this matter but none seem to help me solve my problem. If i remove the code to use CUDA, it runs perfectly but the project i’m working on requires for me to use CUDA.
@nb.jit(nopython=True)
def func_nb():
data = pd.read_csv('dummycsv.csv')
df = DataFrame(data)
df['Tag'] = df['Tag'].map(lambda x: re.sub(r'W+', '_', x))
tempdata = pd.DataFrame(columns=['Time','Value'])
taglist = pd.read_excel('dummyexcel.xlsx')
dt = DataFrame(taglist, columns=['Tag'])
dt['Tag'] = dt['Tag'].map(lambda x: re.sub(r'W+', '_', x))
for i,row in dt.iterrows():
tempdata = tempdata[0:0]
for index,row in df.iterrows():
tag = dt.iloc[i]['Tag']
x = df.iloc[index]['Tag']
val = df.iloc[index]
Expected results are new files being created as a result from reading both excel and csv file and filtering the data in them.
Any help would be greatly appreciated!
Answers:
You will have to let func_nb()
return the df and make sure to call the function func_nb()
def func_nb():
data = pd.read_csv('dummycsv.csv')
df = DataFrame(data)
df['Tag'] = df['Tag'].map(lambda x: re.sub(r'W+', '_', x))
return df
df = func_nb()
After that you can use your dataframe, because it now exists outside of the your function.
I don’t know why “df” is not defined because i already assigned df a value. I read some question and answers online regarding this matter but none seem to help me solve my problem. If i remove the code to use CUDA, it runs perfectly but the project i’m working on requires for me to use CUDA.
@nb.jit(nopython=True)
def func_nb():
data = pd.read_csv('dummycsv.csv')
df = DataFrame(data)
df['Tag'] = df['Tag'].map(lambda x: re.sub(r'W+', '_', x))
tempdata = pd.DataFrame(columns=['Time','Value'])
taglist = pd.read_excel('dummyexcel.xlsx')
dt = DataFrame(taglist, columns=['Tag'])
dt['Tag'] = dt['Tag'].map(lambda x: re.sub(r'W+', '_', x))
for i,row in dt.iterrows():
tempdata = tempdata[0:0]
for index,row in df.iterrows():
tag = dt.iloc[i]['Tag']
x = df.iloc[index]['Tag']
val = df.iloc[index]
Expected results are new files being created as a result from reading both excel and csv file and filtering the data in them.
Any help would be greatly appreciated!
You will have to let func_nb()
return the df and make sure to call the function func_nb()
def func_nb():
data = pd.read_csv('dummycsv.csv')
df = DataFrame(data)
df['Tag'] = df['Tag'].map(lambda x: re.sub(r'W+', '_', x))
return df
df = func_nb()
After that you can use your dataframe, because it now exists outside of the your function.