Check if pandas dataframe is subset of other dataframe

后端 未结 3 1259
盖世英雄少女心
盖世英雄少女心 2021-01-11 11:49

I have two Python Pandas dataframes A, B, with the same columns (obviously with different data). I want to check A is a subset of B, that is, all rows of A are contained in

3条回答
  •  天命终不由人
    2021-01-11 12:09

    You also can try:

    ex = pd.DataFrame({"col1": ["banana", "tomato", "apple"],
                   "col2": ["cat", "dog", "kangoo"],
                   "col3": ["tv", "phone", "ps4"]})
    ex2 = ex.iloc[0:2]
    ex2.isin(ex).all().all()
    

    It returns True

    If you try to switch some values such as tv and phone you get a False value

    ex2 = pd.DataFrame({"col1": ["banana", "tomato"],
                   "col2": ["cat", "dog"],
                   "col3": ["phone", "tv"]})
    ex2.isin(ex).all().all()
    >> False
    

提交回复
热议问题