使用merge()连接两个Pandas DataFrames

使用merge()连接两个Pandas DataFrames

让我们看看如何使用merge()函数来连接两个Pandas DataFrames。

merge()

语法: DataFrame.merge(parameters)

参数 :

  • right:数据框架或命名的系列
  • how : {‘left’, ‘right’, ‘outer’, ‘inner’}, default ‘inner’.
  • about:标签或列表
  • left_on : 标签或列表,或类似数组。
  • right_on : 标签或列表,或类似数组。
  • left_index : bool, 默认为False
  • right_index : bool, 默认为False
  • sort : bool, default False
  • suffixes:(str, str)的元组,默认(‘_x’, ‘_y’)
  • copy : bool, 默认为 True
  • indicator: bool或str,默认为False
  • validate : str, optional

返回:两个合并对象的DataFrame。

例子1 :合并两个具有相同数量元素的数据框架。

# importing the module
import pandas as pd
  
# creating the first DataFrame
df1 = pd.DataFrame({"fruit" : ["apple", "banana", "avocado"],
                    "market_price" : [21, 14, 35]})
display("The first DataFrame")
display(df1)
  
# creating the second DataFrame
df2 = pd.DataFrame({"fruit" : ["banana", "apple", "avocado"],
                    "wholesaler_price" : [65, 68, 75]})
display("The second DataFrame")
display(df2)
  
# joining the DataFrames
display("The merged DataFrame")
pd.merge(df1, df2, on = "fruit", how = "inner")

输出 :
使用merge()连接两个Pandas DataFrames

例子2:合并两个元素数量不同的数据框架。

# importing the module
import pandas as pd
  
# creating the first DataFrame
df1 = pd.DataFrame({"fruit" : ["apple", "banana", 
                               "avocado", "grape"],
                    "market_price" : [21, 14, 35, 38]})
display("The first DataFrame")
display(df1)
  
# creating the second DataFrame
df2 = pd.DataFrame({"fruit" : ["apple", "banana", "grape"],
                    "wholesaler_price" : [65, 68, 71]})
display("The second DataFrame")
display(df2)
  
# joining the DataFrames
# here both common DataFrame elements are in df1 and df2, 
# so it extracts apple, banana, grapes from df1 and df2.  
display("The merged DataFrame")
pd.merge(df1, df2, on = "fruit", how = "inner")

输出 :
使用merge()连接两个Pandas DataFrames

如果我们使用how = “Outer”,它会返回df1和df2中的所有元素,但如果元素列是空的,它就会返回NaN值。

pd.merge(df1, df2, on = "fruit", how = "outer")

输出 :
使用merge()连接两个Pandas DataFrames

如果我们使用how = “left”,它会返回所有存在于左边DataFrame的元素。

pd.merge(df1, df2, on = "fruit", how = "left")

输出 :
使用merge()连接两个Pandas DataFrames

如果我们使用how = “right”,它会返回所有存在于右边DataFrame的元素。

pd.merge(df1, df2, on = "fruit", how = "right")

输出 :
使用merge()连接两个Pandas DataFrames

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程