简体   繁体   中英

matching of columns between two pandas dataframe

import pandas as pd
temp1 = pd.DataFrame(index=arange(10), columns=['a','b'])
temp1['a'] = [1,2,2,3,3,4,4,4,9,11]
temp1['b'] = 'B'

temp2 = pd.DataFrame(index=arange(10), columns=['a','b'])
temp2['a'] = [1,2,3,4,5,6,7,8,9,10]
temp2['b'] = 'B'

As the script above, I want to pickup rows from temp1 that column a was not seen at temp2 . I can use %in% in R to do it easily, how can I do it in pandas?

update 01

the output should be one row which column a is 11 and column b is B

您可以使用isin来获取看到指数,然后否定布尔指数:

temp1[~temp1.a.isin(temp2.a)]

You can use isin to perform boolean indexing:

isin will produce a boolean index:

In [95]:

temp1.a.isin(temp2.a)
Out[95]:
0     True
1     True
2     True
3     True
4     True
5     True
6     True
7     True
8     True
9    False
Name: a, dtype: bool

This can then be used as a mask in the final output:

In [94]:
# note the ~ this negates the result so equivalent of NOT
temp1[~temp1.a.isin(temp2.a)]
Out[94]:
    a  b
9  11  B

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM