0

I want to merge 2 dataframes. My code is as follows; import pandas as pd

from IPython.display import display

dt1 = {
    'aa': ['j', 'b', 'e', 'g', 'i', 'c'],
    "ab": [4, 2, 5, 6, 1, 7],
}

dt2 = {
    'aa': ['b', 'e', 'i', 'j', 'c', 'g'],
    "ac": [4, 9, 5, 8, 3, 4],
}
df1 = pd.DataFrame(dt1)
display(df1)

df2 = pd.DataFrame(dt2)
display(df2)

My expected output is like this;

  aa  ab  ac
0  j   4   8
1  b   2   4
2  e   5   9
3  g   6   4
4  i   1   5
5  c   7   3
Ping Zhang
  • 53
  • 4
  • These are questions with one line solutions and have been asked as nauseam. Please vote to close, or do a quick Google search for duplicates before answering. It's the decent thing to do. – cs95 Dec 24 '20 at 22:26

1 Answers1

1

You want to do a left merge to keep every row of df1: pd.merge(df1,df2,'left')

Output:

  aa  ab  ac
0  j   4   8
1  b   2   4
2  e   5   9
3  g   6   4
4  i   1   5
5  c   7   3
Derek O
  • 16,770
  • 4
  • 24
  • 43