pandas 中基于列表的简单过滤操作抛出 'unhashable Series' 错误

Simple filter operation in pandas based on list throws 'unhashable Series' error

当我尝试根据列值之一是否出现在列表中来过滤数据框中的行时,我看到以下错误 -

TypeError: 'Series' objects are mutable, thus they cannot be hashed

我的过滤操作如下:

 staples[staples.layer_l not in colored_vias]

此处,colored_vias 是一个列表,它是 staples 数据框中 layer_l 和 layer_u 列中包含的值的超集。

In [348]: staples
Out[348]: 
    index   x  y_l  length  net  y_u layer_l  color_l layer_u  color_u
0       0   0    0      50  VDD   50    VIA2        0    VIA2        1
1       1   0  150      50  VDD  200    VIA2        0    VIA2       -1
2       2   0  200      50  VDD  250    VIA2       -1    VIA2        0
3       3  20    0      50  VSS   50    VIA2        0    VIA2        1
4       4  20  200      50  VSS  250    VIA2       -1    VIA2        0
5       5  20  250      50  VSS  300    VIA2        0    VIA2        0
6       6  40    0      50  VDD   50    VIA2        0    VIA2        0
7       7  40   50      50  VDD  100    VIA2        0    VIA2        0
8       8  40  100      50  VDD  150    VIA2        0    VIA2        0
9       9  40  250      50  VDD  300    VIA2        0    VIA2        0
10     10  60    0      50  VSS   50    VIA2        0    VIA2        0
11     11  60   50      50  VSS  100    VIA2        0    VIA2        0
12     12  60  100      50  VSS  150    VIA2        0    VIA2        0
13     13  60  250      50  VSS  300    VIA2        0    VIA2        0

感谢任何帮助!

您应该将 .isin 方法与 ~ 一起使用,而不是 not in 运算符。例子-

staples[~staples['layer_l'].isin(colored_vias)]

Example/Demo -

In [2]: df = pd.DataFrame([[1,2],[3,4],[5,6],[7,8],[9,10]],columns=['A','B'])

In [3]: df
Out[3]:
   A   B
0  1   2
1  3   4
2  5   6
3  7   8
4  9  10

In [4]: aset = {2,6,10}

In [5]: df[df['B'].isin(aset)]
Out[5]:
   A   B
0  1   2
2  5   6
4  9  10

In [6]: df[~df['B'].isin(aset)]
Out[6]:
   A  B
1  3  4
3  7  8