用一个公共列合并 Pandas 数据帧并为不匹配的值设置 NaN
要使用公共列合并两个 Pandas 数据帧,请使用 merge() 函数并将 ON 参数设置为列名。要为不匹配的值设置 NaN,请使用“how”参数并将其设置为left 或 right。这意味着在左侧或右侧合并。
首先,让我们导入 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 合并数据帧。左侧“显示左侧数据帧的所有值,并为第 2 个数据帧中未匹配的值设置 NaN −
mergedRes = pd.merge(dataFrame1, dataFrame2, on ='Car', how ="left")
示例
以下为代码
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 "left" sets NaN for unmatched values from second DataFrame mergedRes = pd.merge(dataFrame1, dataFrame2, on ='Car', how ="left") print("\nMerged data frame with common column...\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 data frame with common column... Car Units Reg_Price 0 BMW 100 7000.0 1 Lexus 150 1500.0 2 Audi 110 NaN 3 Mustang 80 8000.0 4 Bentley 110 NaN 5 Jaguar 90 6000.0
广告