I would like to merge multiple data frames in the sequence of df3 - df2 - df1. df3 and df2 are merged first by df3(id, Feature 1) on df2(id, Feature 1). However then, df2(id, Feature 2) and df1(id, Feature 2) are merged together. The output would look like:
Here is my code:
import pandas as pd
data1 = {
'id': ['1', '2', '3', '4', '5'],
'Feature1': ['K', 'C', 'E', 'G', 'I'],
'Feature2': ['L', 'N', 'F', 'H', 'J']}
df1 = pd.DataFrame(data1, columns = ['id', 'Feature1', 'Feature2'])
data2 = {
'id': ['1', '2', '6', '7', '8'],
'Feature1': ['K', 'M', 'O', 'Q', 'S'],
'Feature2': ['L', 'N', 'P', 'R', 'T']}
df2 = pd.DataFrame(data2, columns = ['id', 'Feature1', 'Feature2'])
data3 = {
'id': ['1', '2', '3', '4', '5', '7', '8', '9', '10', '11'],
'Feature1': ['K', 'M', 'O', 'Q', 'S','X','Y','Z','W','P'],
'Feature2': ['B', 'D', 'F', 'H', 'J','O', 'Q', 'S','X','Y'],
'Feature3': [12, 13, 14, 'K', 'M','S', 'Q',15, 16, 17,]}
df3 = pd.DataFrame(data3, columns = ['id', 'Feature1', 'Feature2','Feature3'])
df1, df2, and df3 are shown in the figure above.
Could I ask how to do it? Thanks!
