1

I have two dataframes df1['LicId'] and df2['LicId'].

df1['LicId'] will always have one value

    LicId
0   abc1234

However df2['LicId'] will have several Ids

    LicId
0   abc1234
1   xyz2345

My task is to compare df1['LicId'] with df2['LicId'] and execute the code only if there is a match between two.

I have tried:

if df1['LicId'][0]==df2['LicId'][0]:
    remaining code

However, this will check only for abc1234 - It has to check for all the Index values of df2. Later, I may also have xyz2345 in df1.

Can someone let me know how to handle this?

4
  • Sorry but your question is very poorly worded. I have no idea what you want to do. Commented Aug 24, 2016 at 15:34
  • Sorry about that - How to compare the value of one dataframe with the values of other dataframe? I have to execute my code only these two values are equal. Commented Aug 24, 2016 at 15:47
  • Can you post samples of your dataframes along with the desired output? Commented Aug 24, 2016 at 15:47
  • if df2 is relatively small in size, you can just check if df1.LicId[0] in df2.LicId.values: ..., or just use merge: if len(df1.merge(df2)): ... Commented Aug 24, 2016 at 16:32

1 Answer 1

1

You can match values with isin():

df1 = pd.DataFrame({'LicId':['abc1234', 'a', 'b']})
df2 = pd.DataFrame({'LicId':['abc1234', 'xyz2345', 'a', 'c']})

df1:

     LicId
0  abc1234
1        a
2        b

df2:

     LicId
0  abc1234
1  xyz2345
2        a
3        c

Matching values:

if len(df2.loc[df2['LicId'].isin(df1['LicId'])]) > 0:
    print(df2.loc[df2['LicId'].isin(df1['LicId'])])
    #remaining code

Output:

     LicId
0  abc1234
2        a
Sign up to request clarification or add additional context in comments.

1 Comment

As a side note isin will require both index & column labels matching for dataframe

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.