import numpy as np import pandas as pd 1. df1=pd.DataFrame({"name":["wangchenyang","guanchenhao","dongshuai"], "age":[30,32,33]}) df2=pd.DataFrame({"name":["wangchenyang","guanchenhao","dongshuai"], "tall":[310,312,313], "age":[130,132,133] }) print(pd.merge(df1,df2,on="name"))#通过name归并,指定归并的主键 #2.左右归并 a1=np.array([[120,130],[110,120],[230,250],[100,98],[98,97]]) data1=pd.DataFrame(a1,columns=["A","B"], index=["语文","数学","英语","理综","计算机"]) a2=np.array([[120,30],[110,120],[230,250],[100,98],[98,97]]) data2=pd.DataFrame(a2,columns=["C","D"], index=["语文","数学","英语","理综","计算机"]) print(pd.merge(data1,data2,left_on="A",right_on="D"))#左右合并两个没有共同点的数据 #3. df1=pd.DataFrame({"name":["A","B","C"], "age":[30,32,33]}) df2=pd.DataFrame({"name":["D","A","E"], "tall":[310,312,313], }) print(pd.merge(df1,df2))#内归并,缺失的丢掉 print(pd.merge(df1,df2,how="outer"))#内归并,缺失的填NaN print(pd.merge(df1,df2,how="left"))#以左边为准 print(pd.merge(df1,df2,how="right"))#以右边为准 #.列冲突 df1=pd.DataFrame({"name":["A","B","C"], "age":[30,32,33]}) df2=pd.DataFrame({"name":["A","B","C"], "age":[310,312,313], }) print("-------------------") print(pd.merge(df1,df2,on="name",suffixes=["_x","_y"]))#解决列冲突