1

I load data from a csv file. When I try to save the labels column into a variable I am getting the error saying label, and the dataset was taken from https://www.kaggle.com/c/digit-recognizer/data

d0 = pd.read_csv('./test.csv')

print(d0.head(5)) # print first five rows of d0.

# to save the labels into a variable l.
l = (d0['label'])

got output:

   pixel0  pixel1  pixel2  pixel3  pixel4  pixel5  pixel6  pixel7  pixel8  \
0       0       0       0       0       0       0       0       0       0   
1       0       0       0       0       0       0       0       0       0   
2       0       0       0       0       0       0       0       0       0   
3       0       0       0       0       0       0       0       0       0   
4       0       0       0       0       0       0       0       0       0   

   pixel9    ...     pixel774  pixel775  pixel776  pixel777  pixel778  \
0       0    ...            0         0         0         0         0   
1       0    ...            0         0         0         0         0   
2       0    ...            0         0         0         0         0   
3       0    ...            0         0         0         0         0   
4       0    ...            0         0         0         0         0   

   pixel779  pixel780  pixel781  pixel782  pixel783  
0         0         0         0         0         0  
1         0         0         0         0         0  
2         0         0         0         0         0  
3         0         0         0         0         0  
4         0         0         0         0         0  

[5 rows x 784 columns]

I'm getting error

KeyError Traceback (most recent call last) ~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 3077 try: -> 3078 return self._engine.get_loc(key) 3079 except KeyError: KeyError: 'label'
During handling of the above exception, another exception occurred:

KeyError Traceback (most recent call last) in 15 16 # save the labels into a variable l. ---> 17 l = (d0['label']) 18 19 # Drop the label feature and store the pixel data in d.

KeyError: 'label'

I cant understand the error described in anaconda

Dropped some error code as stack is showing add more detail error

Please help me on solving this problem

xiawi
  • 1,772
  • 4
  • 19
  • 21
Nnandan15
  • 7
  • 1
  • 5
  • Are you checking for key `label` before you put it in a variable. This situation explores it better: https://stackoverflow.com/a/3845371/1290185 – Priya Ranjan Singh Jun 15 '19 at 19:38
  • I did this: if label in d0: print("yes") else: print("no") and got "no" as output – Nnandan15 Jun 16 '19 at 16:43
  • I did this: if label in d0: print("yes") else: print("no") and got "no" as output I have already posted the total output in the post and it contain the label as pixel0,pixel1,...……… what do I have to do to contain all of them in "label" and thank you for answering – Nnandan15 Jun 16 '19 at 16:49

0 Answers0