如何在pd.Dataframe()中选择数字列

时间:2018-08-29 02:54:30

标签: python pandas index-error

在项目中,“房屋”是源数据,其中包含多个dtype,例如“ float / int”,“ object”。 使用houses.dtypes方法,我们得到了:

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

实际上,我想在“房屋”中找到数字列,因此我使用了以下代码:

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

最后,我得到了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

是否有任何修复此错误的提示?谢谢!

0 个答案:

没有答案