Python Pandas: Nameerror: Name Is Not Defined

Python Name Error. Name is not defined while using Pandas dataframe

The Exception says it all: "name 'temp' is not defined". So the variable temp is not defined before it is used.

The problem with your code is here:

if tag == 'r':
if tok_doc[i].endswith('ly'):
temp = tok_doc[i].replace("ly", "")
# else: temp = None
else:
temp = lemmatizer.lemmatize(tok_doc[i], pos=tag)
lemmatized.append(temp)

If tag == 'r' is True and tok_doc[i].endswith('ly') is not True then temp never gets defined.

Consider adding an else clause like the one I inserted and commented out.

Why do I get "NameError: name 'df' is not defined"?

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.

`NameError: name column_name is not defined`

You need an argument for your query function

When you call apply function what you receive is a pandas series and based on the value of your axis argument you get row or column. When you set the value of axis to 1 you get a row.

So when you call

df['res'] = df[['uid','api_url']].apply(query, axis = 1)

you get each row of you dataframe as argument of the query function.

So you need to modify your query function to

def query(x):
jso = {"id": x.uid,"docs": {"doc1":x.api_url,"type": "US","advanced": {"details":true}}}
json_d = json_dumps(jso)
headers = {"Content-Type": "application/json"}
response = requests.post("https://example.net/document",headers=headers,json=json_d)
return response.text

Your second case query_api works because it has an argument and it will be the column of your data frame.

Hope this explanation helps.

P.S.

In your first apply call you have query() as argument. I assume that was a typo. You only need to specify the name of the function.

EDIT

I have added an example to further explanation

>>> df
uid api_url
0 1 abc.com
1 2 xyz.com

>>> def query(x):
... return str(x.uid) + x.api_url

>>> df.apply(query, axis =1)
0 1abc.com
1 2xyz.com
dtype: object

Pandas - NameError: name 'df2' is not defined

This happens because df2 is defined inside your function and it obeys python scope of variables, so it exists only inside your function definition.

Since you return it and pass another name to it:

common_words = get_top_n_bigram_Group2(df[i], 500)

So you return df2 value and pass it as common_words

And then you are iterating over it:

for word in common_words:

Therefore you should use word, instead of df2 in your printing and writerow functions.



Related Topics



Leave a reply



Submit