2

Hello Im having troubles merging two Pandas dataframes, tryed merge and concat but I just cannot get the format I want, Im using 2 row indexes and all columns names are the same :

    df1                                     df2

           high   low                              high low
 year ind                                 year ind
 2000  A    9      5                      2000  B    8    3      
 2001  A    5      2                      2001  B    4    2

I want something like :

   df3                                  

           high   low                         
 year ind                                
 2000  A     9     5    
 2000  B     8     3                      
 2001  A     5     2            
 2001  B     4     2

Any idea ? Thanks

0

3 Answers 3

2

Use concat + sort_index:

df = pd.concat([df1, df2]).sort_index()
print (df)
          high  low
year ind           
2000 A       9    5
     B       8    3
2001 A       5    2
     B       4    2
Sign up to request clarification or add additional context in comments.

Comments

1

Append here

df1.append(df2)
Out[149]: 
          high   low
year ind            
2000 A       9     5
2001 A       5     2
2000 B       9     5
2001 B       5  1000

Comments

0

From pandas.merge docstring,

Docstring: Merge DataFrame objects by performing a database-style join operation by columns or indexes.

it likely aims at merging two dataframes horizontally. If you just want to have fun, you can do the trick as following. However, this might be a silly solution but if did the work.

pd.merge(df1.T, df2.T, left_index=True, right_index=True).T

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.