pandas过滤ValueError:操作数无法与形状一起广播

时间:2018-02-14 20:04:58

标签: python pandas

我正在尝试使用for循环中的itseld过滤数据帧:

for idx, row in df.iterrows():
    res1 = df[(df.index != idx) & (df.start >= row.start) & (df.end <= row.end)]
    res = pd.concat([res, res1])

但循环的第一行(过滤)让我回答:

ValueError:操作数无法与形状(2920,)(2921,)

一起广播

我不知道为什么

数据框架形成:

df = pd.DataFrame(columns=['start','end','seq','record','len','ir_1','ir_2'])

数据添加如下:

with l_lock:
    new_element = [ir_start, ir_end,ir_seq, record.id, ir_len, seq_q, seq_q_prime]
    df.loc[len(df)] = new_element

有不同的线程,也许它与它有关。

我不能做的是按照帖子第一部分所述进行过滤

如果我跑

df = df.reset_index(drop=True)
在循环之前

,我得到的错误是:

IRMatcher.py:235: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
  res1 = df[(df.index != idx) & (df.start >= row.start) & (df.end <= row.end)]
99% 
Traceback (most recent call last):
  File "IRMatcher.py", line 235, in <module>
    res1 = df[(df.index != idx) & (df.start >= row.start) & (df.end <= row.end)]
  File "/home/trigo/runs/irmatcher/venv/local/lib/python2.7/site-packages/pandas/core/frame.py", line 2133, in __getitem__
    return self._getitem_array(key)
  File "/home/trigo/runs/irmatcher/venv/local/lib/python2.7/site-packages/pandas/core/frame.py", line 2173, in _getitem_array
    key = check_bool_indexer(self.index, key)
  File "/home/trigo/runs/irmatcher/venv/local/lib/python2.7/site-packages/pandas/core/indexing.py", line 2023, in check_bool_indexer
    raise IndexingError('Unalignable boolean Series provided as '
pandas.core.indexing.IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match

1 个答案:

答案 0 :(得分:0)

IIUC:使用列表存储你的临时df

l=[]
for idx, row in df.iterrows():
    res1 = df[(df.index != idx) & (df.start >= row.start) & (df.end <= row.end)]
    l.append(res1)

pd.concat(l)