1

I have 2 dataframes like the following (A and B):

     ID    color
0     5    red
1     6    blue
2     7    blue
3     8    NaN
4     9    green
5     10   NaN

     ID    characteristic
0     7    tall
1     9    short
2     24   short
3     1    tall
4     11   medium
5     10   tall

for each ID in df A I want to check if there is a corresponding ID in dataframe B . If there is a corresponding ID, then new column in df A should have the value True, and if not then False

The output should look like this:

     ID    color  df_b_presence
0     5    red    False
1     6    blue   False
2     7    blue   True
3     8    NaN    False
4     9    green  True
5     10   NaN    True
SeaBean
  • 22,547
  • 3
  • 13
  • 25
mmkb
  • 47
  • 4
  • Sorry I misunderstood the question here, it looks like all you need is an `isin` check. – cs95 Oct 03 '21 at 22:50

1 Answers1

3

You can use .isin(), as follows:

A['df_b_presence'] = A['ID'].isin(B['ID'])

Result:

print(A)

    ID  color  df_b_presence
0   5    red          False
1   6   blue          False
2   7   blue           True
3   8    NaN          False
4   9  green           True
5  10    NaN           True
SeaBean
  • 22,547
  • 3
  • 13
  • 25
  • Curious if this satisfies the per-id requirement that I think OP had ... not sure though – cs95 Oct 03 '21 at 22:26
  • @cs95 My interpretation for per-id is for checking on each row on the `ID` of dataframe `A` for same `ID` in dataframe `B`. Yup, same not sure though. – SeaBean Oct 03 '21 at 22:30
  • Is this question a duplicate of this one: [Pandas populate new dataframe column based on matching columns in another dataframe](https://stackoverflow.com/questions/39816671/pandas-populate-new-dataframe-column-based-on-matching-columns-in-another-datafr)? – Bill Oct 03 '21 at 22:31
  • @Bill Seems not exactly. That question is to bring along getting some column values from another dataframe. This question is on checking existence of same key/value in another dataframe. – SeaBean Oct 03 '21 at 22:37
  • @cs95 Even though there can be duplicated key of `ID` in dataframe A, it just check once more with the same result for each duplicate. Still same result, I think :-) – SeaBean Oct 03 '21 at 22:44
  • 1
    I guess you are right huh! – cs95 Oct 03 '21 at 22:49