0

I am trying to merge data frames in python on Fruit, the two tables are below. any suggestions would be great I have tried a variety of things but I cant get it to work. The below two tables are what I currently have.

Thank you in advance.

Table 1:

Fruit    |    appl | orange |  kiwi
Shop     |         |        |         
 :-----------------------------------:                   
1        |    NaN  | 0.275  |   NaN
2        |     0.4 | -0.600 |   0.2
3        |    0.7  |   NaN  |   NaN
4        |    NaN  |   NaN  |   0.2
5        |    0.3  | 0.350  |   NaN
6        |    NaN  | 0.250  |   NaN
7        |    NaN  |   NaN  |   0.2
8        |    0.4  | 0.400  |   NaN

Table 2:

Fruit    |    kiwi| orange |  appl 
 :-----------------------------------:                   
   x     |    0.4 | 0.275  |   0.2

Final Product:

Fruit    |    appl | orange |  kiwi
Shop     |         |        |         
 :-----------------------------------:                   
1        |    NaN  | 0.275  |   NaN
2        |     0.4 | -0.600 |   0.2
3        |    0.7  |   NaN  |   NaN
4        |    NaN  |   NaN  |   0.2
5        |    0.3  | 0.350  |   NaN
6        |    NaN  | 0.250  |   NaN
7        |    NaN  |   NaN  |   0.2
8        |    0.4  | 0.400  |   NaN
 :-----------------------------------:                   
   x     |    0.2  | 0.275  |   0.4
2
  • Can you format your table better, and provide some data that is immediately readable from Python? Expected output would also be helpful. Commented Jan 5, 2018 at 16:22
  • @Georgy so far I have tried to use pandas but struggling to get the result I want. Commented Jan 5, 2018 at 16:34

2 Answers 2

1

To append one dataframe to another with same column order use:

df1.reindex_axis(sorted(df1.columns), axis=1).append(df2.reindex_axis(sorted(df2.columns), axis=1))

Please take note that Fruit Shop and Fruit columns should be set as index of their respective dataframe.

Sign up to request clarification or add additional context in comments.

2 Comments

different names in dfs Fruit and fruit shop, you did not handle that
@pyd Added that those should be set to index.
0

If I understand your problem correct you could use pd.concat. In your case table_final = pd.concat([table1, table2]). If not specified the concatenation is done on column names, so the column ordering does not matter - apple will be merged with apple.

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.