Filter on a list of values in Pandas
We have a data frame df with an id column with unique identifiers.
Typically, you would have these set as the index of the data frame.
In that case, you can pass a list of index values to the .loc() function:
ids = [502, 3, 165, 174, 184, 14317, 195, 228, 15255, 308, 487, 263, 14283]
df.loc[ids]
However, in some cases the indices may be stored in a plain column. If we want to find a specific id, we can create a view on the data frame:
df[df.id == '502']
But the following does not work because the length of id and ids does not match up for a pairwise comparison:
# Does NOT work
df[df.id == ids]
Instead, we can use isin as follows:
ids = [502, 3, 165, 174, 184, 14317, 195, 228, 15255, 308, 487, 263, 14283]
df[df.id.isin(ids)]
By definition of our problem, these id values do not necessarily correspond to row indices. To get the row indices, we may extend on the same trick as follows:
row_idx = np.nonzero((df.id.isin(ids)).values)[0]
Where (df.id.isin(ids)).values returns a boolean array and np.nonzero returns the rows with a True value (False is interpreted as a 0).
The extra zero-indexing is just because np.nonzero returns a tuple.