0
    Traceback (most recent call last):
  File "data_visualization.py", line 14, in <module>
    y = df.target
  File "C:\Users\Aeryes\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\generic.py", line 4376, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'target'

I keep getting this error when I run the following code or try to use df.target in any situation:

x = df.text
y = df.target
from sklearn.cross_validation import train_test_split
SEED = 2000
x_train, x_validation_and_test, y_train, y_validation_and_test = train_test_split(x, y, test_size=.02, random_state=SEED)
x_validation, x_test, y_validation, y_test = train_test_split(x_validation_and_test, y_validation_and_test, test_size=.5, random_state=SEED)
print("Train set has total {0} entries with {1:.2f}% negative, {2:.2f}% positive".format(len(x_train), 
                                            (len(x_train[y_train == 0]) / (len(x_train)*1.))*100,(len(x_train[y_train == 1]) / (len(x_train)*1.))*100))
print("Validation set has total {0} entries with {1:.2f}% negative, {2:.2f}% positive".format(len(x_validation), 
                                            (len(x_validation[y_validation == 0]) / (len(x_validation)*1.))*100,(len(x_validation[y_validation == 1]) / (len(x_validation)*1.))*100))
print("Test set has total {0} entries with {1:.2f}% negative, {2:.2f}% positive".format(len(x_test), 
                                            (len(x_test[y_test == 0]) / (len(x_test)*1.))*100, (len(x_test[y_test == 1]) / (len(x_test)*1.))*100))

How can I fix this error and continue with my data visualization?

df.head() as requested:

                                                text
0  awww that bummer you shoulda got david carr of...
1  is upset that he can not update his facebook b...
2  dived many times for the ball managed to save ...
3     my whole body feels itchy and like its on fire
4  no it not behaving at all mad why am here beca...
7
  • 1
    what is the output of df.columns?, or better yet 'target' in df.columns? Commented Sep 30, 2018 at 16:18
  • Possible duplicate of Data-frame Object has no Attribute Commented Sep 30, 2018 at 16:20
  • Index(['text'], dtype='object') is the output when I print it to std out. Commented Sep 30, 2018 at 16:23
  • Please copy paste the output of print(df.head()) Commented Sep 30, 2018 at 16:33
  • 1
    The DataFrame you have doesn't have a column named target, I am not sure what we can do about that. You should check your assumptions or data. Commented Sep 30, 2018 at 16:50

0

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.