Maxim Dunavicher
Maxim Dunavicher

Reputation: 635

a way to check if data frame contains a smaller data frame

I am new to data frames so I apologize if the question is obvious ,Assume I have a data frame that looks like that:

  1 2 3
  4 5 6
  7 8 9

and I would like to check if it contains the following data frame:

  5 6
  8 9

is there any build in function in pandas.dataframe that do it?

Upvotes: 0

Views: 167

Answers (1)

Anzel
Anzel

Reputation: 20553

Supposed two dataframes have the same relative columns and index (I assume so as they are dataframe not just values array), here is a quick solution (not the most elegant or efficient) where you compare two dataframes after combine_first:

DataFrame.combine_first(other)

Combine two DataFrame objects and default to non-null values in frame calling the method. Result index columns will be the union of the respective indexes and columns

Example:

df
   a  b  c
0  1  2  3
1  4  5  6
2  7  8  9

df1
   a  b
1  4  5
2  7  8

all(df1.combine_first(df) == df.combine_first(df1))
True

or, if you want to check df1 (smaller) is in df (you know their size already):

all(df == df1.combine_first(df))
True

Upvotes: 1

Related Questions