Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

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

like image 249
Wanderer Avatar asked Sep 04 '26 07:09

Wanderer


1 Answers

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.

like image 111
pissall Avatar answered Sep 05 '26 20:09

pissall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!