Filter data with some conditions

Viewed 60

I have a dataframe

username   score
1          0.0008
1          0.1
1          0.000009
2          0.2
2          0.0098
2          0.7
3          0.99
3          0.019
3          0.0001

I need to filter using that condition

d = {1: 0.05, 2: 0.01, 3: 0.02}

So I need to extract all that less than value in dict for every client.

Desire output:

username   score
1          0.0008
1          0.000009
2          0.0098
3          0.019
3          0.0001

How can I filter it fast? I know it's possible to do it using loops, but I have a huge dataset includes more than 100 usernames.

2 Answers

You can map the username with your dictionary d and then with boolean indexing, only select those that have score less than the mapping result:

df[df.score.lt(df.username.map(d))]

to get

   username     score
0         1  0.000800
2         1  0.000009
4         2  0.009800
7         3  0.019000
8         3  0.000100

Doing this with a list

sample =  [(1, 0.0008), (1, 0.1), (1, 0.000009), (2, 0.2)]
d = {1: 0.05, 2: 0.01, 3: 0.02}


def check(el):
    return True if el[1] < d.get(el[0]) else False


result = filter(check, sample)
Related