0

I'm trying to identify the rows between 2 df which shared the same values for some columns for the SAME row.

Example:

import pandas as pd
df = pd.DataFrame([{'energy': 'power', 'id': '123'}, {'energy': 'gas', 'id': '456'}])
df2 = pd.DataFrame([{'energy': 'power', 'id': '456'}, {'energy': 'power', 'id': '123'}])

df =

   energy   id
0  power  123
1    gas  456

df2 =

   energy     id
0  power    456
1  power    123

Therefore, I'm trying to get the rows from df where energy & id matches exactly in the same row in df2. If I do like this, I get a false result:

df2.loc[(df2['energy'].isin(df['energy'])) & (df2['id'].isin(df['id']))]

because this will match the 2 rows of df2 whereas I would expect only power / 123 to be matched

How should I do to do boolean indexing with multiple "dynamic" conditions based on another df rows and matching the values for the same rows in the other df ?

Hope it's clear

1
  • 1
    Looks like an inner merge: df.merge(df2) ? of course you can select the common keys to join Commented Mar 14, 2021 at 16:27

1 Answer 1

2
pd.merge(df, df2, on=['id','energy'], how='inner')
Sign up to request clarification or add additional context in comments.

1 Comment

Yes that works like this indeed, if you want to do both standard Boolean indexing and merge you need 2 lines instead of 1 but it works great I thought there would be another option but let’s keep it like this then. Thanks

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.