簡體   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