LetsPlayYahtzee LetsPlayYahtzee - 1 year ago 49
Python Question

Apply condition on pandas columns to create a boolen indexing array

I want to drop specific rows from a pandas dataframe. Usually you can do that using something like

df[df['some_column'] != 1234]

df['some_column'] != 1234
does is creating a truth array that is indexing the new df, thus letting only rows with value
to be present.

But in some cases, like mine, I don't see how I can express the condition in such a way, and iterating over pandas rows is way too slow to be considered a viable option.

To be more specific, I want to drop all rows where the value of a column is also a key in a dictionary, in a similar manner with the example above.

In a perfect world I would consider something like

df[df['some_column'] not in my_dict.keys()]

Which is obviously not working. Any suggestions?

Answer Source

What you're looking for is isin

import pandas as pd

df = pd.DataFrame([[1, 2], [1, 3], [4, 6],[5,7],[8,9]], columns=['A', 'B'])
In[9]: df
Out[9]: df
   A  B
0  1  2
1  1  3
2  4  6
3  5  7
4  8  9
mydict = {1:'A',8:'B'}
   A  B
0  1  2
1  1  3
4  8  9