熊猫中的类别明智的值分配

时间:2019-03-19 10:39:53

标签: python pandas

在我的数据框中,我有两列。 Emp_id和城市。数据框的总大小为320万,包含多个城市名称。数据框看起来像-

 ---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-22-541ccb88da6a> in <module>()
      2 df = pd.read_excel(file)
      3 cols = df.columns
----> 4 df = pd.read_excel(file, usecols = ['col1', 'col2', 'col with unicode à'])

C:\Users\GiacomoSachs\Anaconda2\lib\site-packages\pandas\util\_decorators.pyc in wrapper(*args, **kwargs)
    186                 else:
    187                     kwargs[new_arg_name] = new_arg_value
--> 188             return func(*args, **kwargs)
    189         return wrapper
    190     return _deprecate_kwarg

C:\Users\GiacomoSachs\Anaconda2\lib\site-packages\pandas\util\_decorators.pyc in wrapper(*args, **kwargs)
    186                 else:
    187                     kwargs[new_arg_name] = new_arg_value
--> 188             return func(*args, **kwargs)
    189         return wrapper
    190     return _deprecate_kwarg

C:\Users\GiacomoSachs\Anaconda2\lib\site-packages\pandas\io\excel.pyc in read_excel(io, sheet_name, header, names, index_col, parse_cols, usecols, squeeze, dtype, engine, converters, true_values, false_values, skiprows, nrows, na_values, keep_default_na, verbose, parse_dates, date_parser, thousands, comment, skip_footer, skipfooter, convert_float, mangle_dupe_cols, **kwds)
    373         convert_float=convert_float,
    374         mangle_dupe_cols=mangle_dupe_cols,
--> 375         **kwds)
    376 
    377 

C:\Users\GiacomoSachs\Anaconda2\lib\site-packages\pandas\io\excel.pyc in parse(self, sheet_name, header, names, index_col, usecols, squeeze, converters, true_values, false_values, skiprows, nrows, na_values, parse_dates, date_parser, thousands, comment, skipfooter, convert_float, mangle_dupe_cols, **kwds)
    716                                   convert_float=convert_float,
    717                                   mangle_dupe_cols=mangle_dupe_cols,
--> 718                                   **kwds)
    719 
    720     @property

C:\Users\GiacomoSachs\Anaconda2\lib\site-packages\pandas\io\excel.pyc in parse(self, sheet_name, header, names, index_col, usecols, squeeze, dtype, true_values, false_values, skiprows, nrows, na_values, verbose, parse_dates, date_parser, thousands, comment, skipfooter, convert_float, mangle_dupe_cols, **kwds)
    599                                     usecols=usecols,
    600                                     mangle_dupe_cols=mangle_dupe_cols,
--> 601                                     **kwds)
    602 
    603                 output[asheetname] = parser.read(nrows=nrows)

C:\Users\GiacomoSachs\Anaconda2\lib\site-packages\pandas\io\parsers.pyc in TextParser(*args, **kwds)
   2154     """
   2155     kwds['engine'] = 'python'
-> 2156     return TextFileReader(*args, **kwds)
   2157 
   2158 

C:\Users\GiacomoSachs\Anaconda2\lib\site-packages\pandas\io\parsers.pyc in __init__(self, f, engine, **kwds)
    893             self.options['has_index_names'] = kwds['has_index_names']
    894 
--> 895         self._make_engine(self.engine)
    896 
    897     def close(self):

C:\Users\GiacomoSachs\Anaconda2\lib\site-packages\pandas\io\parsers.pyc in _make_engine(self, engine)
   1130                                  ' "c", "python", or' ' "python-fwf")'.format(
   1131                                      engine=engine))
-> 1132             self._engine = klass(self.f, **self.options)
   1133 
   1134     def _failover_to_python(self):

C:\Users\GiacomoSachs\Anaconda2\lib\site-packages\pandas\io\parsers.pyc in __init__(self, f, **kwds)
   2236         self._col_indices = None
   2237         (self.columns, self.num_original_columns,
-> 2238          self.unnamed_cols) = self._infer_columns()
   2239 
   2240         # Now self.columns has the set of columns that we will process.

C:\Users\GiacomoSachs\Anaconda2\lib\site-packages\pandas\io\parsers.pyc in _infer_columns(self)
   2609                 columns = [names]
   2610             else:
-> 2611                 columns = self._handle_usecols(columns, columns[0])
   2612         else:
   2613             try:

C:\Users\GiacomoSachs\Anaconda2\lib\site-packages\pandas\io\parsers.pyc in _handle_usecols(self, columns, usecols_key)
   2669                             col_indices.append(usecols_key.index(col))
   2670                         except ValueError:
-> 2671                             _validate_usecols_names(self.usecols, usecols_key)
   2672                     else:
   2673                         col_indices.append(col)

C:\Users\GiacomoSachs\Anaconda2\lib\site-packages\pandas\io\parsers.pyc in _validate_usecols_names(usecols, names)
   1235         raise ValueError(
   1236             "Usecols do not match columns, "
-> 1237             "columns expected but not found: {missing}".format(missing=missing)
   1238         )
   1239 

ValueError: Usecols do not match columns, columns expected but not found: ['col with unicode \xc3\xa0']

我的最终输出看起来像-

emp_id         city
  2            New York
  3            Houston
  6            Dallas
  7            New York
  11           Dallas
  12           Austin
  13           San Jose
  14           Boston
  15           Boston
  16           Columbus
  24           Austin
  30           Austin

我到目前为止已经完成了-

emp_id         city              present
  2            New York             1
  3            Houston              0
  6            Dallas               1
  7            New York             1
  11           Dallas               1
  12           Austin               0
  13           San Jose             0
  14           Boston               1
  15           Boston               1
  16           Columbus             0
  24           Austin               0
  30           Austin               0

我只考虑3个城市为“ 1”,其余城市为“ 0”

1 个答案:

答案 0 :(得分:0)

您可以这样做:

df['present'] = np.where(df['city'].isin(['New York','Dallas','Boston']),1,0)