Filtering pandas data frame by a list of id's
You could use the isin
method:
In [30]: df = pd.DataFrame({'subscriber_id':[1,2,3,4,5]})
In [31]: df
Out[31]:
subscriber_id
0 1
1 2
2 3
3 4
4 5
[5 rows x 1 columns]
In [32]: mask = df['subscriber_id'].isin([2,4,5])
In [33]: mask
Out[33]:
0 False
1 True
2 False
3 True
4 True
Name: subscriber_id, dtype: bool
In [34]: df.loc[~mask]
Out[34]:
subscriber_id
0 1
2 3
[2 rows x 1 columns]
If you use df.mask
, then the input must be a boolean NDFrame or an array. lambda x: x['subscriber_id'] not in subscribers
is a function, which is why it raised an exception.
Here is one way you could use df.mask
, again with isin
to form the boolean condition:
In [43]: df['subscriber_id'].mask(df['subscriber_id'].isin([2,4,5]).values)
Out[43]:
0 1
1 NaN
2 3
3 NaN
4 NaN
Name: subscriber_id, dtype: float64