Reputation: 149
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
Upvotes: 0
Views: 64
Reputation: 2849
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.
Upvotes: 0
Reputation: 27869
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.
Upvotes: 1