简体   繁体   中英

Python Pandas: NameError: name is not defined

Ok, this is my first Python Pandas program and I'm having a hard time figuring out what the column name is so I can reference it in a function call.

Below is my code. parseDeviceType is calling a function to parse useragentstring. But when I call it using what I think the column name is, I get an error that name is not defined:

df = pd.read_csv('user_agent_strings.txt',index_col=None, na_values=['NA'],sep=',')
dt=parseDeviceType(user_agent_string)
print df.columns

NameError: name 'user_agent_string' is not defined
Index([u'user_agent_string'], dtype='object')

And here's the header and first row of data from the input file containing the useragentstrings:

"user_agent_string"
"Mozilla/5.0 (iPad; CPU OS 7_1_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D201 Safari/9537.53"

Can you help me understand how to reference the column name in the dt=parseDeviceType(user_agent_string) call? I'd like to also know how to reference it by column number if that is possible in a call to a function.

Thanks

Import pandas package to read data

import pandas as pd 

df = pd.read_csv('user_agent_strings', index_col=None, na_values=['NA'],sep=',')

The first thing you need to understand is the error message you are seeing:

NameError is a Python exception and is not related to Pandas in this case. You can get exactly the same error by trying to use any name which the interpreter doesn't know about:

>>> b = a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined

It is important to know that very few Python commands will "magically" create names. To create a name, you would almost always need an assignment ( name = ... ). So as a general rule if you you haven't done this, name will not exist. In your code, the name you have created is df , so you will need to go through that to get to your data.

You can use two different ways to access the data in the dataframe, which are equivalent: df['user_agent_string'] or df.user_agent_string . I recommend trying this out in an interactive environment so that you can see the results before passing it to a function.

I'm also going to guess that your function parseDeviceType only does this for one string (based on the comments), but you want to call this function on every item in your file. To do this you would need apply :

parsed_types = df.user_agent_string.apply(parseDeviceType)

To access columns by number instead of name (which I don't recommend), you can use iloc . This allows you to access all the rows ( : ) and the first colum ( 0 ) from the dataframe object:

user_agent_string = df.iloc[:, 0]

In here , you didnt import the pandas

import pandas as pd
pd = pd.read_csv('anythinf.csv')

When you save your file and open later this time you will get same error so you need to restart your project and compile your code from the start ...if you use jupyter notebook.. you can easily erase all the errors

Try to remove .txt from your file name might help. Like the following:

df = pd.read_csv('user_agent_strings', index_col=None, na_values=['NA'],sep=',')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM