Python Pandas – 将 DataFrame 与多对一关系合并
要合并 Pandas DataFrame,请使用 merge() 函数。通过在 merge() 函数的 “validate” 参数下进行设置,在两个 DataFrames 上实现 多对一关系,即 −
validate = “many-to-one” or validate = “m:1”
多对一关系检查 merge 键在右侧数据集中的唯一性。
首先,让我们创建我们的第 1 个 DataFrame −
dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Audi', 'Mustang', 'Bentley', 'Jaguar'],"Units": [100, 110, 80, 110, 90] } )
现在,让我们创建我们的第 2 个 DataFrame −
dataFrame2 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],"Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000] } )
示例
以下是代码 −
# # Merge Pandas DataFrame with many-to-one relation # import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Audi', 'Mustang', 'Bentley', 'Jaguar'],"Units": [100, 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 "many-to-one" in "validate" parameter mergedRes = pd.merge(dataFrame1, dataFrame2, validate ="many_to_one") print("\nMerged dataframe with many-to-one relation...\n", mergedRes)
输出
这将产生以下输出 −
DataFrame1 ... Car Units 0 BMW 100 1 Audi 110 2 Mustang 80 3 Bentley 110 4 Jaguar 90 DataFrame1 ... Car Reg_Price 0 BMW 7000 1 Lexus 1500 2 Tesla 5000 3 Mustang 8000 4 Mercedes 9000 5 Jaguar 6000 Merged dataframe with many-to-one relation... Car Units Reg_Price 0 BMW 100 7000 1 Mustang 80 8000 2 Jaguar 90 6000
广告