3

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
  • Can you post a sample of your csv? Commented Apr 30, 2018 at 11:55
  • Give a reproducible sample of the data Commented Apr 30, 2018 at 12:06
  • @pissall I added it Commented Apr 30, 2018 at 12:13
  • Looks like if a column is named 'labels', df.labels returns the error: AttributeError: 'DataFrame' object has no attribute 'labels' but df['labels'] works perfectly fine. Commented Sep 19, 2019 at 13:09

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I am still getting an error, KeyError: 'Text', KeyError: 'Text', KeyError: 'label'
Show the output of your df.head()
I added it to the post
Sorry but I don't get it, what's wrong? thanks in advance
Updated @user7631183

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.