使用pd.merge()合并 类似 MySQL 中表和表直接的合并merge与concat的区别在于,merge需要依据某一共同的行或列来进行合并使用pd.merge()合并时,会自动根据两者相同column名称的那一列,作为key来进行合并每一列元素的顺序不要求一致 1. 一对一合并 df1 = pd.DataFrame({"name":["张三","李四","王五"],"id":[1,2,3],"age":[22,33,44], }) df2 = pd.DataFrame({"id":[2,3,4],"sex":["男","女","男"],"job":["Saler","CEO","Programer"] }) display(df1,df2) nameidage0张三1221李四2332王五344 idsexjob02男Saler13女CEO24男Programer # 依据某一共同的列来进行合并 pd.merge(df1,df2) nameidagesexjob0李四233男Saler1王五344女CEO df1.merge(df2) nameidagesexjob0李四233男Saler1王五344女CEO 2. 多对一合并 df3 = pd.DataFrame({"name":["张三","李四","王五"],"id":[1,2,2],"age":[22,33,44], }) df4 = pd.DataFrame({"id":[2,3,4],"sex":["男","女","男"],"job":["Saler","CEO","Programer"] }) display