Python - 连接不重复的 Pandas DataFrames
要连接DataFrame,请使用concat()方法,但要忽略重复项,请使用drop_duplicates()方法。
导入所需的库-
import pandas as pd
创建要连接的数据帧-
#创建DataFrame1 dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Jaguar', 'Audi', 'Mustang'],"Units": [100, 150, 110, 80] } ) #创建DataFrame2 dataFrame2 = pd.DataFrame( { "Car": ['Tesla', 'Jaguar', 'Mercedes', 'Mustang'],"Units": [120, 150, 180, 80] } )
现在,让我们连接DataFrame并删除重复项-
concatRes = pd.concat([dataFrame1, dataFrame2]).drop_duplicates()
示例
以下是代码-
import pandas as pd #创建DataFrame1 dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Jaguar', 'Audi', 'Mustang'],"Units": [100, 150, 110, 80] } ) print"DataFrame1 ...\n",dataFrame1 #创建DataFrame2 dataFrame2 = pd.DataFrame( { "Car": ['Tesla', 'Jaguar', 'Mercedes', 'Mustang'],"Units": [120, 150, 180, 80] } ) print"\nDataFrame2 ...\n",dataFrame2 #连接数据帧并删除重复项 concatRes = pd.concat([dataFrame1, dataFrame2]).drop_duplicates() print"\nConcatenating DataFrame without duplicates...\n", concatRes输出结果
这将产生以下输出-
DataFrame1 ... Car Units 0 BMW 100 1 Jaguar 150 2 Audi 110 3 Mustang 80 DataFrame2 ... Car Units 0 Tesla 120 1 Jaguar 150 2 Mercedes 180 3 Mustang 80 Concatenating DataFrame without duplicates... Car Units 0 BMW 100 1 Jaguar 150 2 Audi 110 3 Mustang 80 0 Tesla 120 2 Mercedes 180