keyerror при доступе к уникальным столбцам - PullRequest
0 голосов
/ 12 сентября 2018
df = df_X_train
i = 1
col = df[str(cat_cols[i])].unique()
print 'outside loop'
print cat_cols[i]
print col
for i in range(len(cat_cols)):
    print "inside loop"
    print i, cat_cols[i]
    col = df[str(cat_cols[i])].unique()
    df = pre.create_df(col, cat_cols[i])
    frames.append(df)
df_all_uniques = pd.concat(frames,axis = 1)
return df_all_uniques

Вот мой код Python. У меня возникла ошибка при доступе к уникальной функции в строке

col = df[str(cat_cols[i])].unique()

, когда я использую эту строку вне цикла for со значением i, равным 1 (i= 1), работает нормально, но внутри цикла for выдает ошибку ключа, приведенную ниже.

outside loop
ISO
[u'FI' u'CH' u'DE' u'US' u'FR' u'IT' u'RU' u'CN' u'TR' u'GB' u'SE' u'CA'
 u'NL' u'DK' u'CZ' u'IE' u'JP' u'TH' u'RO' u'VN' u'BE' u'MT' u'IN' u'SI'
 u'ES' u'HK' u'GR' u'EE' u'MX' u'UA' u'LU' u'PH' u'HU' u'LT' u'MA' u'AE'
 u'IL' u'NO' u'KZ' u'SG' u'LV' u'AT' u'PT' u'EG' u'DM' u'HR' u'DO' u'CY'
 u'PL' u'ID' u'KW' u'AU' u'MY' u'TW' u'CU' u'LI' u'KR' u'TN' u'BY' u'BG'
 u'SA' u'NZ' u'LK' u'AR' u'CL' u'IS' u'BR' u'MD']
inside loop
0 Business Deductible
inside loop
1 ISO

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-75-d81e43d4ec30> in <module>()
     15     print "inside loop"
     16     print i, cat_cols[i]
---> 17     col = df[str(cat_cols[i])].unique()
     18     df = pre.create_df(col, cat_cols[i])
     19     frames.append(df)

/usr/local/lib/python2.7/dist-packages/pandas/core/frame.pyc in __getitem__(self, key)
   2683             return self._getitem_multilevel(key)
   2684         else:
-> 2685             return self._getitem_column(key)
   2686 
   2687     def _getitem_column(self, key):

/usr/local/lib/python2.7/dist-packages/pandas/core/frame.pyc in _getitem_column(self, key)
   2690         # get column
   2691         if self.columns.is_unique:
-> 2692             return self._get_item_cache(key)
   2693 
   2694         # duplicate columns & possible reduce dimensionality

/usr/local/lib/python2.7/dist-packages/pandas/core/generic.pyc in _get_item_cache(self, item)
   2484         res = cache.get(item)
   2485         if res is None:
-> 2486             values = self._data.get(item)
   2487             res = self._box_item_values(item, values)
   2488             cache[item] = res

/usr/local/lib/python2.7/dist-packages/pandas/core/internals.pyc in get(self, item, fastpath)
   4113 
   4114             if not isna(item):
-> 4115                 loc = self.items.get_loc(item)
   4116             else:
   4117                 indexer = np.arange(len(self.items))[isna(self.items)]

/usr/local/lib/python2.7/dist-packages/pandas/core/indexes/base.pyc in get_loc(self, key, method, tolerance)
   3063                 return self._engine.get_loc(key)
   3064             except KeyError:
-> 3065                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   3066 
   3067         indexer = self.get_indexer([key], method=method, tolerance=tolerance)

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'ISO'
...