To use a list of values to select rows from a Python Pandas dataframe, we call the isin
method.
For instanmce, we write
df = pd.DataFrame({'A': [5,6,3,4], 'B': [1,2,3,5]})
r = df[df['A'].isin([3, 6])]
to create the df
data frame and get the values from column 'A'
that’s in rows 3 to 6 with isin
.
We can also get the rows that aren’t in 3 to 6 with
df[~df['A'].isin([3, 6])]