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
merge? Willdf[~df.X1.isin(df2.X1)]work?Happy Coding.