Skip to content Skip to sidebar Skip to footer

Merge Multiple Dataframe Pandas

I try to merge multiple new dataFrames in a main one. Suppose main dataframe: key1 key2 0 0.365803 0.259112 1 0.086869 0.589834 2 0.269619 0.183644 3

Solution 1:

First append or concat both DataFrames together and then merge:

dat = pd.concat([data1, data2], ignore_index=True)

Or:

dat = data1.append(data2, ignore_index=True)

print (dat)
       key1      key2 new feature
00.3658030.259112       info1
10.2040090.669371       info2

#if same joined columns names better is only on parameterdf = test.merge(dat, on=['key1', 'key2'], how='left')

print (df)
       key1      key2 new feature
0  0.365803  0.259112       info1
1  0.086869  0.589834         NaN
2  0.269619  0.183644         NaN
3  0.755826  0.045187         NaN
4  0.204009  0.669371       info2

Solution 2:

You can use pd.DataFrame.update instead:

# create new column and set index
res = test.assign(newfeature=None).set_index(['key1', 'key2'])

# update with new data sequentially
res.update(data1.set_index(['key1', 'key2']))
res.update(data2.set_index(['key1', 'key2']))

# reset index to recover columns
res = res.reset_index()

print(res)

       key1      key2 newfeature
00.3658030.259112      info1
10.0868690.589834None20.2696190.183644None30.7558260.045187None40.2040090.669371      info2

Solution 3:

You can also set the data frames to the same index and use simple loc

df  = df.set_index(["key1", "key2"])
df2 = df2.set_index(["key1", "key2"])

Then

df.loc[:, "new_feature"] = df2['new_feature']

Post a Comment for "Merge Multiple Dataframe Pandas"