我有两个熊猫数据帧,它们有一些相同的行。

假设dataframe2是dataframe1的子集。

我怎么能得到dataframe1的行不在dataframe2?

df1 = pandas.DataFrame(data = {'col1' : [1, 2, 3, 4, 5], 'col2' : [10, 11, 12, 13, 14]}) 
df2 = pandas.DataFrame(data = {'col1' : [1, 2, 3], 'col2' : [10, 11, 12]})

df1

   col1  col2
0     1    10
1     2    11
2     3    12
3     4    13
4     5    14

df2

   col1  col2
0     1    10
1     2    11
2     3    12

预期结果:

   col1  col2
3     4    13
4     5    14

当前回答

当前选择的解决方案产生不正确的结果。为了正确地解决这个问题,我们可以执行从df1到df2的左连接,确保首先只获得df2的唯一行。

首先,我们需要修改原始的DataFrame,添加有data的行[3,10]。

df1 = pd.DataFrame(data = {'col1' : [1, 2, 3, 4, 5, 3], 
                           'col2' : [10, 11, 12, 13, 14, 10]}) 
df2 = pd.DataFrame(data = {'col1' : [1, 2, 3],
                           'col2' : [10, 11, 12]})

df1

   col1  col2
0     1    10
1     2    11
2     3    12
3     4    13
4     5    14
5     3    10

df2

   col1  col2
0     1    10
1     2    11
2     3    12

执行左连接,消除df2中的重复,以便df1的每一行都与df2的一行连接。使用参数指示符返回一个额外的列,指示该行来自哪个表。

df_all = df1.merge(df2.drop_duplicates(), on=['col1','col2'], 
                   how='left', indicator=True)
df_all

   col1  col2     _merge
0     1    10       both
1     2    11       both
2     3    12       both
3     4    13  left_only
4     5    14  left_only
5     3    10  left_only

创建一个布尔条件:

df_all['_merge'] == 'left_only'

0    False
1    False
2    False
3     True
4     True
5     True
Name: _merge, dtype: bool

为什么其他解决方案是错误的

一些解决方案会犯同样的错误——它们只检查每个值在每一列中是独立的,而不是在同一行中一起。添加最后一行,它是唯一的,但有df2中两列的值,这暴露了错误:

common = df1.merge(df2,on=['col1','col2'])
(~df1.col1.isin(common.col1))&(~df1.col2.isin(common.col2))
0    False
1    False
2    False
3     True
4     True
5    False
dtype: bool

这个解决方案得到了同样的错误结果:

df1.isin(df2.to_dict('l')).all(1)

其他回答

如前所述,isin要求列和索引必须相同才能进行匹配。如果match只适用于行内容,一种获取掩码的方法是将行转换为(Multi)Index:

In [77]: df1 = pandas.DataFrame(data = {'col1' : [1, 2, 3, 4, 5, 3], 'col2' : [10, 11, 12, 13, 14, 10]})
In [78]: df2 = pandas.DataFrame(data = {'col1' : [1, 3, 4], 'col2' : [10, 12, 13]})
In [79]: df1.loc[~df1.set_index(list(df1.columns)).index.isin(df2.set_index(list(df2.columns)).index)]
Out[79]:
   col1  col2
1     2    11
4     5    14
5     3    10

如果需要考虑索引,set_index有关键字参数append来将列追加到现有索引。如果列没有对齐,list(df.columns)可以替换为列规范以对齐数据。

pandas.MultiIndex.from_tuples(df<N>.to_records(index = False).tolist())

也可以用来创建指数,但我怀疑这是否更有效。

我这样做的方法包括添加一个新的列,该列对一个数据框架是唯一的,并使用它来选择是否保留一个条目

df2[col3] = 1
df1 = pd.merge(df_1, df_2, on=['field_x', 'field_y'], how = 'outer')
df1['Empt'].fillna(0, inplace=True)

这使得df1中的每个条目都有一个代码-如果它对df1是唯一的,则为0,如果它在两个数据框架中都是1。然后使用它来限制您想要的内容

answer = nonuni[nonuni['Empt'] == 0]

这是最好的方法:

df = df1.drop_duplicates().merge(df2.drop_duplicates(), on=df2.columns.to_list(), 
                   how='left', indicator=True)
df.loc[df._merge=='left_only',df.columns!='_merge']

注意,drop duplicate用于最小化比较。没有他们也可以。最好的方法是比较行内容本身,而不是索引或一/两列,同样的代码也可以用于其他过滤器,如'both'和'right_only',以达到类似的结果。对于这种语法,数据帧可以有任意数量的列,甚至可以有不同的索引。只有列应该出现在两个数据框架中。

为什么这是最好的方法?

索引。差异只适用于基于唯一索引的比较 Pandas.concat()与drop_duplication()结合使用并不理想,因为它还会删除可能只存在于你想保留的数据帧中的行,并出于合理的原因进行复制。

更容易,更简单,更优雅

uncommon_indices = np.setdiff1d(df1.index.values, df2.index.values)
new_df = df1.loc[uncommon_indices,:]

使用merge函数提取不同的行

df = df1.merge(df2.drop_duplicates(), on=['col1','col2'], 
               how='left', indicator=True)

在CSV中保存不同的行

df[df['_merge'] == 'left_only'].to_csv('output.csv')