0

In the project, 'houses' is the source data, which contains multiple dtypes like 'float/int', 'object'. With houses.dtypes methods, we got following:

Order               int64
PID                 int64
MS SubClass         int64
MS Zoning          object
Lot Frontage      float64
Lot Area            int64
Street             object
...                  ...

Actually, I wanna locate the numeric columns in 'houses', thus I used this code:

houses_numeric = houses[(houses.dtypes=='int64')|(houses.dtypes=='float64')]

Finally, I got the IndexError:

IndexingError                             Traceback (most recent call last)
<ipython-input-50-33e1ea213da1> in <module>()
----> 1 houses_numeric = houses[(houses.dtypes=='int64')|(houses.dtypes=='float64')]
      2 #(houses.dtypes=='int64')|(houses.dtypes=='float64')
      3 #houses[(houses.dtypes=='int64')|(houses.dtypes=='float64')]

/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2680         if isinstance(key, (Series, np.ndarray, Index, list)):
   2681             # either boolean or fancy integer index
-> 2682             return self._getitem_array(key)
   2683         elif isinstance(key, DataFrame):
   2684             return self._getitem_frame(key)

/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py in _getitem_array(self, key)
   2720             # check_bool_indexer will throw exception if Series key cannot
   2721             # be reindexed to match DataFrame rows
-> 2722             key = check_bool_indexer(self.index, key)
   2723             indexer = key.nonzero()[0]
   2724             return self._take(indexer, axis=0)

/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py in check_bool_indexer(ax, key)
   2354         mask = isna(result._values)
   2355         if mask.any():
-> 2356             raise IndexingError('Unalignable boolean Series provided as '
   2357                                 'indexer (index of the boolean Series and of '
   2358                                 'the indexed object do not match')

IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match

Are there any hints to fix this error? Thanks!

leo022
  • 925
  • 6
  • 7
  • Indexing without `loc` will align along the rows, where as you are selecting the columns, so the lengths are different. Does `houses_numeric = houses.loc[ :, (houses.dtypes=='int64')|(houses.dtypes=='float64')]` work for you? – johnchase Aug 29 '18 at 02:58
  • This post will help you: https://stackoverflow.com/questions/22470690/get-list-of-pandas-dataframe-columns-based-on-data-type – Joseph Seung Jae Dollar Aug 29 '18 at 03:01
  • You can use also: [`select_dtypes`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.select_dtypes.html) – U13-Forward Aug 29 '18 at 03:07

0 Answers0