1

The following is my code:

np.random.seed(0)
left = pd.DataFrame({'key': ['A', 'B', 'C', 'D'], 'value': np.random.randn(4)})    
right = pd.DataFrame({'key': ['B', 'D', 'E', 'F'], 'value': np.random.randn(4)})
print(pd.merge(left,right))

The result is:

Empty DataFrame
Columns: [key, value]
Index: []

I don't know why they are empty.

2
  • 1
    What data is in your dataframes? Maybe there are no matching values between them? Commented Mar 11, 2021 at 0:02
  • 1
    Use on= parameter: print(pd.merge(left,right, on='key')) Commented Mar 11, 2021 at 0:02

1 Answer 1

2

Try specifying the column on which to join like this:

>>> pd.merge(left, right, on='key')
  key   value_x   value_y
0   B  0.400157  1.867558
1   D  2.240893 -0.977278

If you require all keys:

>>> pd.merge(left, right, on='key', how='outer')
  key   value_x   value_y
0   A  1.764052       NaN
1   B  0.400157  1.867558
2   C  0.978738       NaN
3   D  2.240893 -0.977278
4   E       NaN  0.950088
5   F       NaN -0.151357
Sign up to request clarification or add additional context in comments.

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.