Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

AttributeError: 'DataFrame' object has no attribute 'label'

Writer Matthew Martinez

I created a data frame using the following line:

df = pd.read_csv('/Users/cs213/Desktop/class1.csv', sep = ',', error_bad_lines=False)

and if print the columns as such

print (df.columns) 

I get

Index(['Text', 'label'], dtype='object')

But if I wanted to use the columns as in here

df = df[df.Text.apply(lambda x: x.isnumeric())]
df = df[df.Text.apply(lambda x: x !="")]
df = df[df.label.apply(lambda x: x !="")]

I get the following error:

AttributeError: 'DataFrame' object has no attribute 'label'

I have already tried the solution in here: Data-frame Object has no Attribute and it did not work.

Sample of the CSV file

Here is the output of df.head()enter image description here

4

1 Answer

EDIT 1:

Reproducible sample of your CSV :

df = pd.DataFrame({'Text': [u'Well I am', u"Not my scene", u"Brutal"], 'label': ['y', 'n', 'n']})

The function you are trying to run:

>>> df = pd.DataFrame({'Text': [u'Well I am', u"Not my scene", u"Brutal"], 'label': ['y', 'n', 'n']})
>>> df Text label
0 Well I am y
1 Not my scene n
2 Brutal n
>>> df = df[df['Text'].apply(lambda x: x.isnumeric())]
>>> df
Empty DataFrame
Columns: [Text, label]
Index: []

Of course there will be no attribute 'label'

So what's happening is that, all x.isnumeric() calls return False, and hence none of the data is saved to df. What you are trying to do with df = df[df['Text'].apply(lambda x: x.isnumeric())] is that "In df, what are the rows in which 'Text' is numeric." (Now this returns False). None of the rows are numeric, so you get an empty dataframe.

5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy