0

Is it possible to use merge to df's, df1 and df2, to return only the rows that are Nan in df2? an example:

import pandas as pd
import numpy as np

data = {'X1': ['A', 'B', 'C', 'D', 'E'], 
        'X2': ['meow', 'bark', 'moo', 'squeak', 'cheep']}

data2 = {'X1': ['A', 'B', 'F', 'D', 'E'], 
         'X3': ['cat', 'dog', 'frog', 'mouse', 'chick']}

#df = pd.DataFrame(data, columns = ['name', 'age', 'preTestScore', 
'postTestScore'])
df = pd.DataFrame(data)
df2 = pd.DataFrame(data2)

pd.merge(df, df2, how ='left', on = 'X1')

This returns

X1   X2     X3
A   meow    cat
B   bark    dog
C   moo NaN
D   squeak  mouse
E   cheep   chick

I want a result that looks like:

X1   X2     X3
C   moo NaN

I have tried every combination of merge that I can think of but I can't get it. I know I can achieve the result with lots of manipulations to the merged df, but i was hoping there was a simple efficient answer

3
  • Do you need merge? Will df[~df.X1.isin(df2.X1)] work? Commented Jun 17, 2018 at 0:43
  • @student! Ha! that's what I was looking for! I didn't think I could use isin on a df column like that! Thanks! Commented Jun 17, 2018 at 0:46
  • Great that it worked. Happy Coding. Commented Jun 17, 2018 at 0:47

1 Answer 1

1

Just using isnull and any with your result to filter it

s=pd.merge(df, df2, how ='left', on = 'X1')
s[s.isnull().any(1)]
Out[185]: 
  X1   X2   X3
2  C  moo  NaN
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for that. It gives me another option to use besides students above. I wonder which method is the most efficient time wise
@MickHawkes this work for multiple column match if you need to match two key , I recommend merge rather than Isin

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.