Pandas 数据框外部连接合并
要合并 Pandas 数据框,请使用 merge() 函数。外部连接在两个数据框上实现,通过设置 merge() 函数的 “how” 参数,即:
how = “outer”
首先,让我们导入 pandas 库并为其设置别名:
import pandas as pd
让我们创建 DataFrame1:
dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )
现在,让我们创建 DataFrame2:
dataFrame2 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000] } )
使用公共列 Car 合并数据框,且 "how" 参数中的 "outer" 实现外部连接:
mergedRes = pd.merge(dataFrame1, dataFrame2, on ='Car', how ="outer")
示例
以下是代码:
import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } ) print"DataFrame1 ...\n",dataFrame1 # Create DataFrame2 dataFrame2 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000] } ) print"\nDataFrame2 ...\n",dataFrame2 # merge DataFrames with common column Car and "outer" in "how" parameter implements Outer Join mergedRes = pd.merge(dataFrame1, dataFrame2, on ='Car', how ="outer") print"\nMerged dataframe with outer join...\n", mergedRes
输出
这会产生以下输出:
DataFrame1 ... Car Units 0 BMW 100 1 Lexus 150 2 Audi 110 3 Mustang 80 4 Bentley 110 5 Jaguar 90 DataFrame2 ... Car Reg_Price 0 BMW 7000 1 Lexus 1500 2 Tesla 5000 3 Mustang 8000 4 Mercedes 9000 5 Jaguar 6000 Merged dataframe with outer join... Car Units Reg_Price 0 BMW 100.0 7000.0 1 Lexus 150.0 1500.0 2 Audi 110.0 NaN 3 Mustang 80.0 8000.0 4 Bentley 110.0 NaN 5 Jaguar 90.0 6000.0 6 Tesla NaN 5000.0 7 Mercedes NaN 9000.0
广告