filtering df values on the basis of another variable code example

Example 1: how to apply filters in pandas

# filter rows for year 2002 using  the boolean expression
>gapminder_2002 = gapminder[gapminder.year.eq(2002)]
>print(gapminder_2002.shape)
(142, 6)

Example 2: filter one dataframe by another

df1 = pd.DataFrame({'c': ['A', 'A', 'B', 'C', 'C'],
                    'k': [1, 2, 2, 2, 2],
                    'l': ['a', 'b', 'a', 'a', 'd']})
df2 = pd.DataFrame({'c': ['A', 'C'],
                    'l': ['b', 'a']})
keys = list(df2.columns.values)
i1 = df1.set_index(keys).index
i2 = df2.set_index(keys).index
df1[~i1.isin(i2)]