Search code examples
python-3.xpandasdataframeconcatenationmulti-index

combine multiindex dataframe with Int64Index dataframe


i have one dataframe with multiindex

result =

MultiIndex([(1, 'HK_MN001'),
            (2, 'HK_MN001'),
            (3, 'HK_MN002'),
            (4, 'HK_MN003'),
            (5, 'HK_MN004'),
            (6, 'HK_MN005'),
            (7, 'HK_MN005'),
            (8, 'HK_MN005')],
           names=['ID1', 'ID2'])

Another dataframe with index:

photo_df:

Int64Index([1, 2, 3, 4, 5, 6, 7, 8], dtype='int64', name='ID1')

I want to concatenate both the dataframes but it gives me error: Code:

result = pd.concat([result,photo_df], axis = 1,sort=False)
 
error:
"Can only union MultiIndex with MultiIndex or Index of tuples, "

NotImplementedError: Can only union MultiIndex with MultiIndex or Index of tuples, try mi.to_flat_index().union(other) instead.

Result Dataframe is:

enter image description here

photo_df dataframe is:

   PhotoID                          raw_photo
0        1  HK_MN001_DSC_2160_161014Ushio.JPG
1        2  HK_MN001_DSC_2308_161014Ushio.JPG
2        3  HK_MN002_DSC_2327_161014Ushio.JPG
3        4  HK_MN003_DSC_1474_181015Ushio.jpg
4        5  HK_MN004_DSC_1491_181015Ushio.jpg
5        6  HK_MN005_DSC_1506_181015Ushio.JPG
6        7  HK_MN005_DSC_1527_181015Ushio.JPG
7        8  HK_MN005_DSC_1528_181015Ushio.jpg

Required Output dataframe:(If possible drop the index = Id1) enter image description here


Solution

  • I think you need in both DataFrames create MultiIndex:

    photo_df = photo_df.set_index('PhotoID', drop=False)
    photo_df.columns = pd.MultiIndex.from_product([photo_df.columns, ['']])
    print (photo_df)
            PhotoID                          raw_photo
                                                      
    PhotoID                                           
    1             1  HK_MN001_DSC_2160_161014Ushio.JPG
    2             2  HK_MN001_DSC_2308_161014Ushio.JPG
    3             3  HK_MN002_DSC_2327_161014Ushio.JPG
    4             4  HK_MN003_DSC_1474_181015Ushio.jpg
    5             5  HK_MN004_DSC_1491_181015Ushio.jpg
    6             6  HK_MN005_DSC_1506_181015Ushio.JPG
    7             7  HK_MN005_DSC_1527_181015Ushio.JPG
    8             8  HK_MN005_DSC_1528_181015Ushio.jpg
    
    
    
    #second level ID2 is column
    result = pd.concat([result.reset_index(level=1),photo_df], axis = 1,sort=False)