Green
Green

Reputation: 2565

Filter Pandas Series by dictionary values

I have a pandas Series words which look like:

0             a
1    calculated
2     titration
3         curve
4           for
5        oxalic
6          acid
7            be
8          show
9            at
Name: word, dtype: object

I also have a Series occurances which looks like:

a            278 
show         179
curve         2
Name: index, dtype: object

I want to filter words using occurances in a way that a word would be filtered if it is not in occurances or it value is less than 100.

In the given example I would like to get:

0             a
8          show
Name: word, dtype: object

isin only check existence and when I've tried to use apply\map or [] operator I got an Error

Series objects are mutable and cannot be hashed

I can also work with solution on DataFrames.

Upvotes: 0

Views: 87

Answers (3)

Daniel Labbe
Daniel Labbe

Reputation: 2019

The isin method works, but generate a list of booleans you should use as index:

>> # reproduce the example
>> import pandas as pd
>> words = pd.Series(['a','calculated','titration','curve','for','oxalic','acid','be','show','at'])
>> occurances = pd.Series(['a','show','curve'], index= [278, 179, 2])

>> # apply the filter
>> words[words.isin(occurances[occurances.index > 100])]
0       a
8    show
dtype: object

Upvotes: 0

Celius Stingher
Celius Stingher

Reputation: 18367

I think you would need to first filter the specific words you want from your occurences Series, and then use the index of it, as the value for the .isin():

output = words[words.isin(occurences[occurences > 100].index)]

Upvotes: 4

IoaTzimas
IoaTzimas

Reputation: 10624

Try this:

words[words.apply(lambda x: x not in occurances or (x in occurances and occurances[x]<100))]

Upvotes: 1

Related Questions