Pandas: concat with duplicated index

  concatenation, dataframe, join, pandas, python

I wanted to concat four dataframe. df has unique index and other 3 of them has duplicated values in index.
Following is my code:

import pandas as pd

data = {'id':['1','2','3','4','5','6'],
        'name':['A1','A2','A2','A1','A3','A3'],
        'date_create':['28-01-2020','15-02-2020','15-03-2020',
                       '25-03-2020','01-04-2020','30-05-2020']}
df = pd.DataFrame(data)

data = {'userid':['1','2','3','3','5','6'],
        'likesDate':['28-01-2020','15-02-2020','15-03-2020',
                       '25-03-2020','01-04-2020','30-05-2020']}
df1 = pd.DataFrame(data)

data = {'userid':['1','1','2','4','6','6'],
        'dislikesDate':['8-01-2020','1-02-2020','15-03-2020',
                       '25-03-2020','01-04-2020','30-05-2020']}
df2 = pd.DataFrame(data)

data = {'userid':['1','2','2','4','4','6'],
        'DeleteDate':['28-01-2020','15-02-2020','15-03-2020',
                       '25-03-2020','05-04-2020','23-05-2020']}
df3 = pd.DataFrame(data)

df.set_index('id', inplace=True)
df1.set_index('userid', inplace=True)
df2.set_index('userid', inplace=True)
df3.set_index('userid', inplace=True)

df_final = pd.concat([df,df1, df2,df3],axis=1)

print(df_final)

got this error:

ValueError: Shape of passed values is (7, 5), indices imply (6, 5)

Any suggestion or code example would be great help.

Source: Python Questions

LEAVE A COMMENT