Multiple Tables
Inner Merge
Basic
newDf = pd.merge(df1, df2)
# creates new dataframe with matching columns (based on name) merged into one
# none matching columns have their own column
# or
newDf = df1.merge(df2)
# merge 3
newDf = df1.merge(df2)\
.merge(df3)
# or
newDf = df1.merge(df2).merge(df3)Merge Specific Columns
Its common for different IDs to have the same name, i.e. 'ID'
We dont always want this
pd.merge(
df1,
df2.rename(columns={'id': 'customer_id'}))
pd.merge(
leftDf,
rightDf,
left_on='colName1',
right_on='colName2')
# matches the two columns, new column names will be 'colName', the common chars
# we can give it suffixes
pd.merge(
leftDf,
rightDf,
left_on='colName1',
right_on='colName2',
suffixes=['_suffForLeft', '_suffForRight',])
# just use this one, trust me
# look at link if u care that muchOther merge types
pd.merge(df1, df2, how='outer')
# sets merge to outer
# void values are set to NaN and Null
pd.merge(df1, df2, how='left')
# sets merge to left (df2 may have nulls)
pd.merge(df1, df2, how='right')
# sets merge to right (df1 may have nulls)Concatenate
sticks 2 df with matching columns together
newdf = pd.concat([df1, df2])