xxxxxxxxxx
import pandas as pd
# Create two DataFrames
data1 = {
'id': [1, 2, 3, 4],
'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3'],
'C': ['C0', 'C1', 'C2', 'C3']
}
data2 = {
'id': [1, 2, 3, 4],
'B': ['B0', 'B1', 'B2', 'B3'],
'C': ['C4', 'C5', 'C6', 'C7'],
'D': ['D4', 'D5', 'D6', 'D7']
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
# Find the columns unique to df2
unique_cols_df2 = set(df2.columns) - set(df1.columns)
# Merge the DataFrames on the 'id' column and keep only the unique columns from df2
df_merged = pd.merge(df1, df2[['id'] + list(unique_cols_df2)], on='id')
print(df_merged)
xxxxxxxxxx
df_outer = pd.merge(df1, df2, on='id', how='outer') #here id is common column
df_outer
xxxxxxxxxx
merged_df = DF2.merge(DF1, how = 'inner', on = ['date', 'hours'])
xxxxxxxxxx
#suppose you have two dataframes df1 and df2, and
#you need to merge them along the column id
df_merge_col = pd.merge(df1, df2, on='id')
xxxxxxxxxx
# First dataframe with three columns
dlist=["vx","vy","vz"]
df=pd.DataFrame(columns=dlist)
df["vx"]=df1["v2x"]
df["vy"]=df1["v2y"]
df["vz"]=df1["v2z"]
# second dataframe with three columns
dlist=["vx","vy","vz"]
df0=pd.DataFrame(columns=dlist)
df0["vx"]=df2["v1x"]
df0["vy"]=df2["v1y"]
df0["vz"]=df2["v1z"]
# Here with concat we can create new dataframe with garther both in one
# YOU CAN PUT SOME VALUES IN EACH AND CHECK IT
# WHAT INSIDE THE CONCAT MUST BE A LIST OF DATAFRAME
v = pd.concat([df,df0])