0

I have 2 frames, 1 has continent data, the other metals-group data.

    continents
year  africa  asia
1996    2       3
1997    5       6

    metal-groups
year  ferrous  non-ferrous
1996    2       3
1997    5       6

I need to merge them on 'year', and try keep the categories associated with the columns of the final frame (i.e. continents and metal-groups).

What's the best way to do this?

Dean
  • 151
  • 1
  • 1
  • 10
  • Use `df = pd.concat([df1, df2], axis=1)` – jezrael Aug 20 '20 at 12:21
  • @jezrael - if I execute your code, it doesn't add the categories (continents and metals-group) into the final dataframe. (The original dataframes don't have the category information in them to begin with - I guess this is the problem) – Dean Aug 21 '20 at 06:54
  • Can yu add output from your code to question? Also what is `print (df1.columns)` and `print (df2.columns)` ? – jezrael Aug 21 '20 at 06:55
  • `print(df1.columns)` Index(['Year', 'Iron and Ferro-Alloy Metals', 'Non-Ferrous Metals', 'Precious Metals', 'Industrial Minerals', 'Mineral Fuels'], dtype='object') `print(df2.columns)` Index(['Year', 'Africa', 'Asia', 'Europe', 'Latin America', 'North America', 'Oceania'], dtype='object') – Dean Aug 21 '20 at 07:23
  • Problem is you need `MultiIndex` in columns, how are created `df1, df2` ? – jezrael Aug 21 '20 at 07:25
  • df1 and df2 are 2 different sheets from a .xls file that have been imported as df's. The name of the sheet describes the category. – Dean Aug 21 '20 at 07:50

0 Answers0