SulfoCyaNate
SulfoCyaNate

Reputation: 426

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

When I try to filter rows in a dataframe based on whether one of the column values occurs in a list, I see the following error -

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

My filter operation is the following:

 staples[staples.layer_l not in colored_vias]

Here, colored_vias is a list that is a superset of the value(s) contained in the layer_l and layer_u columns in the staples dataframe.

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

Any help is appreciated!

Upvotes: 0

Views: 298

Answers (1)

Anand S Kumar
Anand S Kumar

Reputation: 90889

You should use .isin method with ~, rather than the not in operator . Example -

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

Upvotes: 1

Related Questions