Home > Blockchain >  How to merge two datasets with different shape (pandas) [duplicate]
How to merge two datasets with different shape (pandas) [duplicate]

Time:10-07

I want to merge two datasets of different sizes. Ex.

df = {'identifier': [1,1,1,2,2,2],
        'Income': [50,40,70,50,60,30],
        }
df2= {'identifier': [1,2],
        'Rent': [150,300],
        }

With the following output:

df3={'identifier': [1,1,1,2,2,2],
     'Income': [50,40,70,50,60,30],
     'Rent': [150,150,150,300,300,300],
    }

CodePudding user response:

You can use an inner join with .merge() with the default inner join option.

Data Setup

df = {'identifier': [1,1,1,2,2,2],
        'Income': [50,40,70,50,60,30],
        }
df2= {'identifier': [1,2],
        'Rent': [150,300],
        }
df = pd.DataFrame(df)    
df2 = pd.DataFrame(df2)    

Main Code:

df3 = df.merge(df2, on='identifier')

Result:

print(df3)

   identifier  Income  Rent
0           1      50   150
1           1      40   150
2           1      70   150
3           2      50   300
4           2      60   300
5           2      30   300

CodePudding user response:

df_merge

  df.merge(df2, on='identifier')
  • Related