-1

I have two dataframes that I'd like to merge:

df1=pd.DataFrame([['a','b','c'],['e','f','g'],['h','i','j']], columns=['X','Y','Z'], index=[10,15,25])
df2=pd.DataFrame([['A','B','C'],['H','I','J']], columns=['X','Y','Z'], index=[12,22])

#df1
    X  Y  Z
10  a  b  c
15  e  f  g
25  h  i  j

#df2
    X  Y  Z
12  A  B  C
22  H  I  J

I'd like to merge them sequentially, such that the rows in df2 are added to df1, on the same columns since columns would contain data from both.

# intended output
    X  Y  Z
10  a  b  c
12  A  B  C   #from df2
15  e  f  g
22  H  I  J   #from df2
25  h  i  j
Sos
  • 1,577
  • 1
  • 15
  • 38

1 Answers1

1

Use df.append with sort_index():

In [2123]: df1.append(df2).sort_index()
Out[2123]: 
    X  Y  Z
10  a  b  c
12  A  B  C
15  e  f  g
22  H  I  J
25  h  i  j
Mayank Porwal
  • 31,737
  • 7
  • 30
  • 50