5
node1    node2    weight  date


3         6     1        2002

2          7     1        1998

2          7     1        2002

2          8     1        1999

2         15     1        2002

9        15     1        1998

2         16     1        2003

2         18     1        2001

I want to delete rows which have the values [3, 7, 18].These values can be in any of the rows node1 or node2.

1
  • Are you sure you want to delete them? Why not filter them out? df[(~df['node1'].isin([3,7,18])) & ~df['node2'].isin([3,7,18])] Commented Oct 27, 2017 at 15:15

1 Answer 1

5
In [8]: new = df[~df.filter(regex='^node').isin([3,7,18]).any(1)]

In [9]: new
Out[9]:
   node1  node2  weight  date
3      2      8       1  1999
4      2     15       1  2002
5      9     15       1  1998
6      2     16       1  2003

step by step:

In [164]: df.filter(regex='^node').isin([3,7,18])
Out[164]:
   node1  node2
0   True  False
1  False   True
2  False   True
3  False  False
4  False  False
5  False  False
6  False  False
7  False   True

In [165]: df.filter(regex='^node').isin([3,7,18]).any(1)
Out[165]:
0     True
1     True
2     True
3    False
4    False
5    False
6    False
7     True
dtype: bool

In [166]: ~df.filter(regex='^node').isin([3,7,18]).any(1)
Out[166]:
0    False
1    False
2    False
3     True
4     True
5     True
6     True
7    False
dtype: bool

In [167]: df[~df.filter(regex='^node').isin([3,7,18]).any(1)]
Out[167]:
   node1  node2  weight  date
3      2      8       1  1999
4      2     15       1  2002
5      9     15       1  1998
6      2     16       1  2003
Sign up to request clarification or add additional context in comments.

2 Comments

No explanation whatsoever?
@FaCoffee, yeah, you are right. I've added all single steps - I believe this is the best explanation... ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.