2016-12-08 2 views
0

안녕하세요, 세 개의 열이있는 데이터 프레임을 사용하고 있습니다 : comment_id, class 및 comment_message 세 열을 저장해야하지만 불리는 열을 저장하려고하면 오류가 발생합니다. class, my complete code 다음과 같이데이터 프레임에서 특정 열을 가져 오는 방법은 무엇입니까?

#Here is the problem 
classification = df1['class'] 

파일이 보인다 : 다음 보이는 : 문제가되어 갔지

from sklearn import svm 
import pandas as pd 
from sklearn.feature_extraction.text import TfidfVectorizer 

df1=pd.read_csv("C:/Users/acamagon/Downloads/dataSet",sep=',') 
#print(df1) 

comment_id = df1['comment_id'] 



comment_message = df1['comment_message'] 
print(comment_message) 

여기 것은

comment_id,comment_message,class  
10154395643583692_10154397346673692,quisiera saber el precio y las caracteristicas del selulae samsung s5 xfavoor,1 
10154395643583692_10154397434578692,"buenos dias, necesito que le den seguimiento a un telefono que deje en garantia desde octubre en el cac urban center de xalapa, veracruz. ya van 4 veces y me dicen que el telefono no esta y ya va para 3 meses que lo deje. espero me den una respuesta pronto. me comunico al *111 y solo me dicen que el folio sigue en pendiente.",1 
10154395643583692_10154397511368692,no sirve su aplicacion de mi telcel... [[PHOTO]],0 
10154395643583692_10154397598508692,"buenas tardes, gracias por su atencion brindada... pude resolver mi duda y asi sabre que es lo mejor para mi. saludos.",1 
10154394898978692_10154397173938692,q precio tiene el plan????,2  
10154394898978692_10154397265133692,para solicitarlo?,1 

내가 woul

--------------------------------------------------------------------------- 
KeyError         Traceback (most recent call last) 
C:\Program Files\Anaconda3\lib\site-packages\pandas\indexes\base.py in get_loc(self, key, method, tolerance) 
    1944    try: 
-> 1945     return self._engine.get_loc(key) 
    1946    except KeyError: 

pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4154)() 

pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4018)() 

pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12368)() 

pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12322)() 

KeyError: 'class' 

During handling of the above exception, another exception occurred: 

KeyError         Traceback (most recent call last) 
<ipython-input-54-f52e2494564b> in <module>() 
    15 
    16 
---> 17 classification = df1['class'] 
    18 
    19 

C:\Program Files\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key) 
    1995    return self._getitem_multilevel(key) 
    1996   else: 
-> 1997    return self._getitem_column(key) 
    1998 
    1999  def _getitem_column(self, key): 

C:\Program Files\Anaconda3\lib\site-packages\pandas\core\frame.py in _getitem_column(self, key) 
    2002   # get column 
    2003   if self.columns.is_unique: 
-> 2004    return self._get_item_cache(key) 
    2005 
    2006   # duplicate columns & possible reduce dimensionality 

C:\Program Files\Anaconda3\lib\site-packages\pandas\core\generic.py in _get_item_cache(self, item) 
    1348   res = cache.get(item) 
    1349   if res is None: 
-> 1350    values = self._data.get(item) 
    1351    res = self._box_item_values(item, values) 
    1352    cache[item] = res 

C:\Program Files\Anaconda3\lib\site-packages\pandas\core\internals.py in get(self, item, fastpath) 
    3288 
    3289    if not isnull(item): 
-> 3290     loc = self.items.get_loc(item) 
    3291    else: 
    3292     indexer = np.arange(len(self.items))[isnull(self.items)] 

C:\Program Files\Anaconda3\lib\site-packages\pandas\indexes\base.py in get_loc(self, key, method, tolerance) 
    1945     return self._engine.get_loc(key) 
    1946    except KeyError: 
-> 1947     return self._engine.get_loc(self._maybe_cast_indexer(key)) 
    1948 
    1949   indexer = self.get_indexer([key], method=method, tolerance=tolerance) 

pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4154)() 

pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4018)() 

pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12368)() 

pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12322)() 

KeyError: 'class' 
+0

df1.head()는 어떤 모양입니까? 아니면 출력물 (df1)을 출력합니까? – dartdog

답변

3

보십시오이 :, 지원에 대한 감사를이 문제를 극복하기 위해

오류를 어떤 제안을 주셔서 감사하고 싶습니다하면 다음과 같다

df1.columns = [c.strip() for c in list(df1.columns.values)] 
print(df1["class"]) 

문제였다 당신의 class 헤더 공백을 포함합니다. 공백 문자를 .strip()으로 제거하면 팬더가 헤더를 찾을 수 있으므로 KeyError을 피할 수 있습니다.

관련 문제