繁体   English   中英

使用 pandas isin 过滤列时项目长度错误

[英]Item wrong length when use pandas isin to filter column

使用 pandas isin 过滤列时,我遇到项目长度错误

这是我的代码

selected_raw_data = raw_data[raw_data.columns.isin(selected['Column'])].copy()

错误信息在这里

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-28-f7f86ab9946e> in <module>
----> 1 selected_raw_data = raw_data[raw_data.columns.isin(selected['Column'])].copy()

/opt/conda/lib/python3.8/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2891         # Do we have a (boolean) 1d indexer?
   2892         if com.is_bool_indexer(key):
-> 2893             return self._getitem_bool_array(key)
   2894 
   2895         # We are left with two options: a single key, and a collection of keys,

/opt/conda/lib/python3.8/site-packages/pandas/core/frame.py in _getitem_bool_array(self, key)
   2937             )
   2938         elif len(key) != len(self.index):
-> 2939             raise ValueError(
   2940                 f"Item wrong length {len(key)} instead of {len(self.index)}."
   2941             )

ValueError: Item wrong length 396 instead of 362.

假设 selected['Column'] 解析为列名的列表或类似列表(如数据框的系列或列),您可以使用:

raw_data[selected['Column']].copy()

筛选选定的列。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM