1

I have two dataframes df1 and df2, I need to check if the values in df1 column x1 and column x2 exist in df2 column x. If the value doesn't exists, then add it to df2 column x and NaN to df2 column y.

The following is the what I have, it works but takes too long for large datasets and I feel it could be improved and simplified using Pandas methods.

df1 = pd.DataFrame({'x1':['a', 'b', 'e'], 'x2':['c', 'd', 'b']})

df2 = pd.DataFrame({'x':['d', 'e', 'f'], 'y':['a1', 'b2', 'c3']})

diff = set([*df1[~df1['x1'].isin(df2['x'])]['x1'], *df1[~df1['x2'].isin(df2['x'])]['x2']])

for x in diff:
    df2 = df2.append({"x":x}, ignore_index=True)

df1:

    x1  x2
0   a   c
1   b   d
2   e   b

df2:

    x   y
0   d   a1
1   e   b2
2   f   c3

Results should be:

x   y
0   d   a1
1   e   b2
2   f   c3
3   c   NaN
4   b   NaN
5   a   NaN

3 Answers 3

2

Here is another way to get it done, using melt + append

melt_ = pd.melt(df1, value_name='x')[["x"]]

df2.append(melt_, ignore_index=True).drop_duplicates('x')

   x    y
0  d   a1
1  e   b2
2  f   c3
3  a  NaN
4  b  NaN
6  c  NaN
Sign up to request clarification or add additional context in comments.

1 Comment

This seems the best way to do it with just utilising pandas, only correction would be: melt_ = pd.melt(df1, value_name='x')[["x"]] df2.append(melt_, ignore_index=True).drop_duplicates('x'). Thanks!
2

Do you want this -

from itertools import chain
value_to_add = set(chain(*df1.values)).difference(df2.x.values)
df2 = pd.concat([df2,pd.DataFrame({'x':list(value_to_add)})]).reset_index(drop=True)

Alternative without itertools chain :

value_to_add = set(df1.values.flatten()).difference(df2.x.values)
df2 = pd.concat([df2,pd.DataFrame({'x':list(value_to_add)})]).reset_index(drop=True)

Comments

1
df2 = pd.concat(
    [
        df2,
        pd.DataFrame({"x": np.setdiff1d(df1, df2["x"])}),
    ]
)
print(df2)  # add .reset_index(drop=True) if you want clean index

Prints:

   x    y
0  d   a1
1  e   b2
2  f   c3
0  a  NaN
1  b  NaN
2  c  NaN

Comments

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.