w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Python Pandas - Remove values from first dataframe if not in second dataframe

I think you can achieve what you want using the isin Series method on each of the columns:

In [11]: te['item'].isin(tr['item']) &
te['user'].isin(tr['user'])
Out[11]:
0    False
1     True
2    False
3     True
4    False
5     True
dtype: bool

In [12]: te[te['item'].isin(tr['item']) & te['user'].isin(tr['user'])]
Out[12]:
   item  user
1    12     2
3    15     1
5    12     1

In 0.13 you'll be able to use the new DataFrame isin method (on current master):

In [21]: te[te.isin(tr.to_dict(outtype='list')).all(1)]
Out[21]:
   item  user
1    12     2
3    15     1
5    12     1

hopefully by release the syntax should be a bit better on release:

te[te.isin(tr).all(1)]




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.