1

In my dataframe (df), for each line, I need drop rows which the value from both node1 and node2 columns are matching with another list (groups).

df = pd.DataFrame(dict(node1=["A","A","B","C","D"], node2=["B","D","E","F","G"]))
> df
    node1   node2
0   A   B
1   A   D
2   B   E
3   C   F
4   D   G

groups = ["A","B","C","F"]
> groups
['A', 'B', 'C', 'F']

In this example, df's row 0 and row 3 have both values matching with my groups list, so I need to drop these rows. My new dataframe would be:

df2 = pd.DataFrame(dict(node1=["A","B","D"], node2=["D","E","G"]))
> df2
    node1   node2
0   A   D
1   B   E
2   D   G
ASF
  • 269
  • 2
  • 10
  • 1
    https://stackoverflow.com/questions/19960077/how-to-filter-pandas-dataframe-using-in-and-not-in-like-in-sql – ALollz Jul 23 '20 at 17:52

2 Answers2

1

try using .isin() and .all(1)

df.loc[~df.isin(groups).all(1)]
#output:
    node1   node2
1   A       D
2   B       E
4   D       G
Terry
  • 2,761
  • 2
  • 14
  • 28
0
   import pandas as pd 
   df = pd.DataFrame(dict(node1=["A","A","B","C","D"], node2=["B","D","E","F","G"]), columns=["node1", "node2"])
   groups = ["A","B","C","F"]

   df = df.loc[~((df["node1"].isin(groups)) & (df["node2"].isin(groups)))]
UGuntupalli
  • 769
  • 1
  • 9
  • 22