在某些列上合并两个 Pandas DataFrame
我们可以使用合并函数合并某些列上的两个 Pandas DataFrame,只需指定要合并的某些列。
Syntax: DataFrame.merge(right, how=’inner’, on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=False, copy=True, indicator=False, validate=None)
示例 1:让我们创建一个数据帧,然后将它们合并为一个数据帧。
创建数据框:
Python3
# importing modules
import pandas as pd
# creating a dataframe
df1 = pd.DataFrame({'Name':['Raju', 'Rani', 'Geeta', 'Sita', 'Sohit'],
'Marks':[80, 90, 75, 88, 59]})
# creating another dataframe with diffrent data
df2 = pd.DataFrame({'Name':['Raju', 'Divya', 'Geeta', 'Sita'],
'Grade':['A', 'A', 'B', 'A'],
'Rank':[3, 1, 4, 2 ],
'Gender':['Male', 'Female', 'Female', 'Female']})
# display df1
display(df1)
# display df2
display(df2)
Python3
# applying merge
df1.merge(df2[['Name', 'Grade', 'Rank']])
Python3
# importing modules
import pandas as pd
# creating a dataframe
df1 = pd.DataFrame({'Name':['Raju', 'Rani', 'Geeta', 'Sita', 'Sohit'],
'Marks':[80, 90, 75, 88, 59]})
# creating another dataframe with diffrent data
df2 = pd.DataFrame({'Name':['Raju', 'Divya', 'Geeta', 'Sita'],
'Grade':['A', 'A', 'B', 'A'],
'Rank':[3, 1, 4, 2 ],
'Gender':['Male', 'Female', 'Female', 'Female']})
# display df1
display(df1)
# display df2
display(df2)
# applying merge with more parameters
df1.merge(df2[['Grade', 'Name']], on = 'Name', how = 'left')
Python3
# importing modules
import pandas as pd
# creating a dataframe
df1 = pd.DataFrame({'Name':['Raju', 'Rani', 'Geeta', 'Sita', 'Sohit'],
'Marks':[80, 90, 75, 88, 59]})
# creating another dataframe with diffrent data
df2 = pd.DataFrame({'Name':['Raju', 'Divya', 'Geeta', 'Sita'],
'Grade':['A', 'A', 'B', 'A'],
'Rank':[3, 1, 4, 2 ],
'Gender':['Male', 'Female', 'Female', 'Female']})
# display df1
display(df1)
# display df2
display(df2)
# applying merge with more parameters
df2.merge(df1[['Marks', 'Name']])
输出:
现在合并数据框:
蟒蛇3
# applying merge
df1.merge(df2[['Name', 'Grade', 'Rank']])
输出:
结果数据框包含 df1 的所有列,但 df2 的某些指定列具有键列 Name,即结果列包含 Name、Marks、Grade、Rank 列。两个数据帧具有不同数量的值,但合并后仅显示两个数据帧中的共同值。
示例 2:在结果数据框中,df2 的 Grade 列基于键列 Name 与 df1 合并,合并类型为 left,即左侧数据框 (df1) 的所有值都将显示。
蟒蛇3
# importing modules
import pandas as pd
# creating a dataframe
df1 = pd.DataFrame({'Name':['Raju', 'Rani', 'Geeta', 'Sita', 'Sohit'],
'Marks':[80, 90, 75, 88, 59]})
# creating another dataframe with diffrent data
df2 = pd.DataFrame({'Name':['Raju', 'Divya', 'Geeta', 'Sita'],
'Grade':['A', 'A', 'B', 'A'],
'Rank':[3, 1, 4, 2 ],
'Gender':['Male', 'Female', 'Female', 'Female']})
# display df1
display(df1)
# display df2
display(df2)
# applying merge with more parameters
df1.merge(df2[['Grade', 'Name']], on = 'Name', how = 'left')
输出:
示例 3:在本示例中,我们将 df1 与 df2 合并。 df1 的 Marks 列与 df2 合并,此处仅显示基于两个数据框中关键列 Name 的公共值。
蟒蛇3
# importing modules
import pandas as pd
# creating a dataframe
df1 = pd.DataFrame({'Name':['Raju', 'Rani', 'Geeta', 'Sita', 'Sohit'],
'Marks':[80, 90, 75, 88, 59]})
# creating another dataframe with diffrent data
df2 = pd.DataFrame({'Name':['Raju', 'Divya', 'Geeta', 'Sita'],
'Grade':['A', 'A', 'B', 'A'],
'Rank':[3, 1, 4, 2 ],
'Gender':['Male', 'Female', 'Female', 'Female']})
# display df1
display(df1)
# display df2
display(df2)
# applying merge with more parameters
df2.merge(df1[['Marks', 'Name']])
输出: