我有两个数据帧。我想看看另一个数据帧中是否存在特定的行(完整的)。来自df_subset的示例行:
id category value date
1 A 10 01-01-15
3 C 10 03-01-15
另一个df_full:
id category value date
1 A 10 01-01-15
2 B 10 02-01-15
3 C 10 03-01-15
4 D 16 04-01-15
有没有办法检查一个数据帧的行是否存在于另一个数据帧中?这样的事情(显然这不起作用):df_subset in df_full
,存在吗?
> True
答案 0 :(得分:3)
我认为你可以merge
使用内部联接(默认情况下)与DataFrame.equals
进行比较,以便与df_subset
进行比较:
print (pd.merge(df_subset,df).equals(df_subset))
True
答案 1 :(得分:2)
您可以使用merge(..., indicator=True)方法:
In [14]: pd.merge(df1, df2, indicator=True, how='outer')
Out[14]:
id category value date _merge
0 1 A 10 01-01-15 both
1 3 C 10 03-01-15 both
2 2 B 10 02-01-15 right_only
3 4 D 16 04-01-15 right_only
答案 2 :(得分:2)
使用numpy
(df_subset.values[:, None] == df_full.values).all(2).any(1).all()
True
解释
# using [:, None] to extend into new dimension at
# take advantage of broadcasting
a1 = df_subset.values[:, None] == df_full.values
# ━> third dimension ━>
# ━━━━> axis=2 ━━━>
# 1st dim
---->[[[ True True True True] # │
[False False True False] # │ second dimension
[False False True False] # │ axis=1
[False False False False]] # ↓
# axis=0
---->[[False False True False] # │
[False False True False] # │ second dimension
[ True True True True] # │ axis=1
[False False False False]]] # ↓
# first row of subset with each row of full
[[[ True True True True] <-- This one is true for all
[False False True False]
[False False True False]
[False False False False]]
# second row of subset with each row of full
[[False False True False]
[False False True False]
[ True True True True] <-- This one is true for all
[False False False False]]]
a2 = a1.all(2)
# ┌─ first row of subset all equal
[[ True False False False]
[False False True False]]
# └─ second row of subset all equal
a3 = a2.any(1)
# ┌─ first row of subset matched at least one row of full
[ True True]
# └─ second row of subset matched at least one row of full
a3.all()
True
df_subset
所有行都在df_full